# Occurrence of a digit in a given number using Java ## Number of times the digit x occurs in the given number

In this program we will count Occurrence of a X in a number using Java . The input may lie within the range of integer.

If the digit does not occur in the input it should print 0 else the count of digits. We can use %  operator to separate or find last digit and by this we can compare with given digit and increment count value. We can also find by converting given number into string and checking each character with given digit.

Sample Input :

Enter a number : 897982
Enter the digit : 9
Output : 2

Explanation : The digit 9 occurs twice
Let’s see the algorithm and Code to find Occurrence of a X in a number using Java

## Algorithm

1.  Start
2. Get the input value from the user.
3. Get the digit from the i/o console.
4. Declare variables x,b,c
• x – Given number
• b – Digit
• c – no. of occurrences
5. Take a for loop.
6. Declare a variable i to iterate over every character of string.
7. Compare each character with the digit
• if  character equals digit increment count.
8. Print the value of count.
9. End

## Java Code:

import java.io.*;
import java.util.*;
class PREPINSTA
{
public static void main (String[] args)
{
Scanner obj = new Scanner(System.in);
int x,c=0;
x = obj.nextInt();
char b = obj.next().charAt(0);
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);
}
}

### Input:

`8979829`

`2`