Question

Write a program that reads numbers from the user, and prints a table showing how many...

Write a program that reads numbers from the user, and prints a table showing how many times each digit appears in each number. The user should be able to enter more than one number to be tested for repeated digits. The program should terminate when the user enters a number that is less than 0. Here is a sample output: Enter a number (-1 to end this loop): 41271092

Digit: 0 1 2 3 4 5 6 7 8 9

Occurrences: 1 2 2 0 1 0 0 1 0 1

Enter a number (-1 to end this loop): 21272092

Digit: 0 1 2 3 4 5 6 7 8 9

Occurrences: 1 1 4 0 0 0 0 1 0 1

Enter a number (-1 to end this loop): 222345

Digit: 0 1 2 3 4 5 6 7 8 9

Occurrences: 0 0 3 1 1 1 0 0 0 0

Enter a number (-1 to end this loop): 98798

Digit: 0 1 2 3 4 5 6 7 8 9

Occurrences: 0 0 0 0 0 0 0 1 2 2

Enter a number (-1 to end this loop): -1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.util.Scanner;
//Driver code
public class CountDigitOccurences {
   public static void main(String[] args) {
       //instantiating the Scanner class
       Scanner scanner = new Scanner(System.in);
       //variable number
       long number;
       //indefinite loop that breaks when user enters -1
       while(true){
           //prompt to enter a number
           System.out.println("Enter a number (-1 to end this loop): ");
           //input the number and store into number
           number = scanner.nextInt();
           //if number is -1, break the loop
           if(number == -1)
               break;
          
           //the array that holds the digit frequencies
           int frequency[]= new int[10];
           int rem = 0;
           //counting frequency of each digit of the number
           //by extracting it as a remainder
           while(number > 0){
               rem = (int)number % 10;
               //increment the index of the frequency
               // of whatever the digit is
               frequency[rem]++;
               number = number / 10;
           }

           // printing it to the output
           System.out.println("Digit: 0 1 2 3 4 5 6 7 8 9");
           System.out.print("Occurences:");
           for (int i = 0; i < frequency.length; i++) {
               System.out.print(frequency[i] + " ");  
           }

           System.out.println();
       }
   }
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a program that reads numbers from the user, and prints a table showing how many...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT