Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
Occurrence of a digit in a given number using Java
October 1, 2022
Occurrence of a digit in a given number using Java
In this page we will discuss the program to find the occurrence of a digit in a given number using java programming language. We are given with a number and a digit and need to count the number of occurrence of that digit in the given number.
Method Discussed :
Method 1 : Using loops.
Method 2 : Using string.
Method 1 :
Declare variable count that will count the required number of occurrences
Take a while loop.
Declare a variable rem to store every digit of the number to be compared.
//Write a program to print the Occurrence of a Digit in a given Number in Java
import java.io.*;
import java.util.*;
class Main{
public static void main (String[] args)
{
int n = 898989, count = 0;
int d = 9;
while(n>0){
int rem = n%10;
if(rem == d)
count++;
n /= 10;
}
System.out.println(count);
}
}
//Write a program to print the Occurrence of a Digit in a given Number in Java
import java.io.*;
import java.util.*;
class Main{
public static void main (String[] args)
{
int x = 898989, c = 0;
char b = '9';
String s = Integer.toString(x);
for(int i=0;i<s.length();i++){
if(s.charAt(i)==b)
c=c+1;
}
System.out.println(c);
}
}
Output:
3
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment