Write a java program, that receives an integer number and a digit from the user. The program displays the number of times the digit occurs in the number.
example: 23456
digit 4 is displayed one time.
CODE TO COUNT THE FREQUENCY OF A DIGIT IN A NUMBER IS AS FOLLOWS:-
import java.util.*; // Header File
public class Main
{
// Method to calculate frequency of a particular digit in a
number
static int countDigits(int numb_input, int digit)
{
// Counter to store the frequency of the
digit
int count = 0;
// Loop will work until the input number reduces to
0
while (numb_input > 0)
{
// checking the digit from number to the input
digit
if (numb_input % 10 == digit)
count++;
// reduce the number by a factor of 10
numb_input = numb_input / 10;
}
return count;
}
// Driver Code
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
// input number numb_input
int numb_input;
System.out.println("Enter the value of number : ");
numb_input = sc.nextInt();
sc.nextLine();
// input digit
int digit;
System.out.println("Enter the value of digit : ");
digit = sc.nextInt();
sc.nextLine();
System.out.println("The frequency is : "+countDigits(numb_input,
digit)); // calling the method
}
}
OUTPUT:-
Get Answers For Free
Most questions answered within 1 hours.