Question

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

Write a c++ 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 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

Here to store digit count array is used. Each index of array stores corresponding digit occurence .

C++ CODE

#include<iostream>
using namespace std;
// COUNT THE OCCURANCE DIGITS OF A NUMBER , USING ARRAY AND ITS INDEX POSITION
//IF A DIGIT 0 IS FOUND THEN 0 INDEX OF ARRAY WILL INCRMENT. THEN PRINT THE ARRAY
int main()
{
   int num; // NU,BER
   int x[10]; //ARRAY TO STORE DIGIT COUNT
   int i,d; // TEMPORARY VAR
   while(1) // LOOP UNTIL USER ENTERS -1
   {
       cout<<"\nEnter a numbber : ";
       cin>>num;
       if(num==-1) // CHECK IF -1 THEN LOOP BREAKS
           break;

       for(i=0; i<10; i++) // INITALLY ASSIGN 0 TO EACH ARRAY ELEMENTS
           x[i]=0;
          
       while (num> 0)
       {
           d=num % 10; // GET INDIVIDUAL DIGIT OF NUMBER
           x[d]++;           // INCREMENT SAME INDEX OF ARRAY, IF DIGIT GET 1 THEN 1 INDEX WILL BE INCREMENTED REPEATEDLY
           num = num / 10; // DECREASE THE NUMBER FROM RIGHT SIDE
       }

       cout<<"\nDigits : ";
       for(i=0; i<10; i++) // PRINT DIGITS
           cout<<i<<" ";
       cout<<"\nOccurrences : ";
       for(i=0; i<10; i++) // PRINT ARRAY THAT CONTAINS THE DIGIT COUNT
           cout<<x[i]<<" ";
   }
   return 0;
} // MAIN END

SCREEN SHOT CODE

SCREEN SHOT OUTPUT

Add a comment
Know the answer?
Add Answer to:
Write a c++ program that reads numbers from the user, and prints a table showing how...
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
  • Write a complete C++ program that do following. 1) Read a positive integer from the user...

    Write a complete C++ program that do following. 1) Read a positive integer from the user with proper prompt. Assume that this user is nice and he/she would not start the integer with digit 0. 2) Create a size 10 array as counters for digits 0 - 9. 3) Use a while loop for processing a) Single out the current last digit using modular 10 technique b) Increment the corresponding counter through the index derived in a). 4) At the...

  • Write a program that prints out multiplication table: 1. In the main loop, it should ask...

    Write a program that prints out multiplication table: 1. In the main loop, it should ask a user to enter a number in the range of 1 to 12. Exit the program upon input out of range (eg., 0 or 13). 2. Output the multiplication table from 1 to 12. Possible output: Enter the number (1-12): 9 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5...

  • 5. Write a program that does to perform the following using: Reads a number num that...

    5. Write a program that does to perform the following using: Reads a number num that is in the range from 0 to 999999999 and a digit n that is in the range from 0 to 9 from the user. Write while loop to force user to enter appropriate values. . Finds how many times the digit n occurs in the number num. Prints the output as shown. Eİ CAWindowsisystem32cmdexe Enter number ( to 999999999): 3545592 the digit S appears...

  • In C++: Write a program that asks the user for an array of X numbers. Your...

    In C++: Write a program that asks the user for an array of X numbers. Your program moves the largest number all the way to the right, and then shows it to the user. Hint: Use a loop to swap X times, but only swap if the number on the left is bigger than the number on the right. Sample Run: How many numbers would you like to enter? 6 Please enter number 1: 10 Please enter number 2: 8...

  • Python: Write a code to ask a number (one digit) from the user and prints the...

    Python: Write a code to ask a number (one digit) from the user and prints the respective row of the multiplication table. For example, if the user enters 5, the following lines will be printed. Note that, if the user enters any number outside the range of 1-9, the program should display an error message. 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • C++ please Problem 3: Write a C++ program that prompts the user to enter an integer...

    C++ please Problem 3: Write a C++ program that prompts the user to enter an integer of any length (but not more than 8- digits), the program then extracts and prints each digit in its respective column. For example, in the integer 436, 4 should be printed in the 4th column, 3 in the 3rd and 6 in the 6th column. Hint: you can use setw to solve this problem. Sample Input/Output Enter an integer of any length. 9836472 ...Printing...

  • Write a program “hw4.c” that reads integer (less than or equal 100) from the keyboard and,...

    Write a program “hw4.c” that reads integer (less than or equal 100) from the keyboard and, on the output, writes the sum of the divisors of n (other than itself). For integers less than or equal to 1 it should print 0. For example, the input -3 0 1 4 5 6 12 should generate the output 0 0 0 3 1 6 16 Explanation of output: The input -3 is less than 1, output is 0. The input 0...

  • Write a C program named space_to_line.c that features a while loop that continuously reads input from...

    Write a C program named space_to_line.c that features a while loop that continuously reads input from the user one character at a time and then prints that character out. The exception is that if the user inputs a space character (‘ ‘), then a newline character (‘\n’) should be printed instead. This will format the output such that every word the user inputs is on its own line. Other than changing the spaces to newlines, the output should exactly match...

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