Question

Write a java program, that receives an integer number and a digit from the user. The...

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.

Homework Answers

Answer #1

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:-

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
[JAVA] Write a program that prompts the user for an integer and displays if the provided...
[JAVA] Write a program that prompts the user for an integer and displays if the provided integer is a prime number or not. A prime number is a number that is divisible only by 1 and itself. First few prime numbers are 2,3,5,7,11,13 and so on. [using if-statements, and if-else-statement ]
Write a program that prompts the user to enter an integer number between 1 and 999....
Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid. Name the program file Q1.cpp Example: if the user enters 12, sum of digits is 3. If the user enters 122, sum of digits is 5.
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
WRITE A JAVA PROGRAM: The last digit of a credit card number is the check digit,...
WRITE A JAVA PROGRAM: The last digit of a credit card number is the check digit, which protects against transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Following steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...
In Java : Randomly generate an integer number in range [10, 99]. Write a program to...
In Java : Randomly generate an integer number in range [10, 99]. Write a program to check if the digit at 10’s position is less than the digit at 0’s position. If yes, swap these 2 digits. Display original number and new number. For example, if the number generated is 53, after process, the new number will be 35. If original number is 12, no process needed. If the number is 50, the new number will be 5.
Write a program that takes n integer numbers from the user, and then counts the number...
Write a program that takes n integer numbers from the user, and then counts the number of even numbers and odd numbers and print them to the screen. Sample Output: Enter how many numbers you have: 10 Enter the 10 numbers: 1 3 19 50 4 10 75 20 68 100 The number of even numbers is: 6 The number of odd numbers is: 4 (( in java ))
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
Write a java program to find the average of several scores read by the user from...
Write a java program to find the average of several scores read by the user from keyboard, the program should stop accepting scores when the user enters a negative score, and then it displays the average for the scores
in c++ Write a C++ program that asks the user to enter an integer number and...
in c++ Write a C++ program that asks the user to enter an integer number and prints it back "vertically" to the screen. Use recursion in your solution. As an example of how the program will work: Please enter an integer: 12345 The integer you entered will print vertically as: 1 2 3 4 5
Write a java program to display a given range of integer numbers (0 to 9) in...
Write a java program to display a given range of integer numbers (0 to 9) in the following order: Example : given (5 to 9), the program displays 56789 67895 78956 89567 95678 The first line displays the min to max range. Each line that follows should display the next higher number except when it hits the maximum number. In that situation, the line wraps back to the minimum sequence. For this problem write a static method to do the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT