Answer:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Tester {
public static void main(String[] args) throws IOException {
Scanner in=new Scanner(System.in);
System.out.println("Enter a Number =======>");
long N ;
while (!in.hasNextLong()) {
System.out.println("That's not a number!");
in.next();
}
N=in.nextLong();
System.out.println("Number Entered is =======>"+N);
System.out.println("Enter a Digit =======>");
int D;
while (!in.hasNextLong()) {
System.out.println("That's not a number!");
in.next();
}
D=in.nextInt();
System.out.println("Digit Entered is =======> "+ D);
long que=N;
int rem;
int count=0;
while(true){
long temp;
temp = (long)que/10;
rem = (int) (que % 10);
System.out.println(temp+" "+ que+" "+rem);
if(rem==D) count++;
que=temp;
if(que==0) break;
}
System.out.println("The number of "+ D+"'s in "+ N + " is "+ count );;
}
}
Explanation:
- Divide the que variable by 10 and assign its result to the temp variable.
- Calculate the remainder.
- Increment the count if the remainder is equal to the value of D variable and assign the value of que to temp variable.