Repetition of a Number Program in Java Solution

Question:-

Pinky’s mom provides Pinky  with a number  and a key digit.  She wants Pinky to find out how many times that key digit appears in that number.  Help Pinky to do that by writing a program.

Sample Input 1:

Enter the number  16466

Enter the key digit   6

Sample Output 1:

6 appears 3 times in 16466

Sample Input 2:

Enter the number  8458

Enter the key digit   6

Sample Output 2:

6 appears 0 times in 8458

Code:-

import java.util.*;
class NumberRepetition{
    public static void main(String[] args){
        int n,d, c=0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number ");
        n=sc.nextInt();
        int temp = n;
        System.out.println("Enter the key digit ");
        d=sc.nextInt();
        while (n>0){
            if(n%10==d)
                c++;
            n=n/10;
        }
        System.out.println(d+" appears "+c+" times in "+temp);
    }
}

Leave a Comment

Scroll to Top