Question

Write a C-program to print a table (shown below) showing how many times a digit appears...

Write a C-program to print a table (shown below) showing how many times a digit appears in a number. The input number will be entered by the user. For example, if the user enters 378677189, then the output should be as follows:

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

Frequency: 0 1 0 1 0 0 1 3 2 1

Remember to allow the users to enter their own choice of input number. [Hint: arrays could be useful. Also, a regular int data type may not be sufficient to hold such a big integer. Hence, think of using long data type.] Your program should start with asking the user to enter a number. Do not hard-code the input number.

Homework Answers

Answer #1
#include <stdio.h>

int main() {
    int i, d, num, n, count;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Digit: ");
    for (i = 0; i < 10; ++i) {
        printf("%d ", i);
    }
    printf("\nFrequency: ");
    for (i = 0; i < 10; ++i) {
        num = n;
        count = 0;
        if (num == 0 && i == 0) {
            count = 1;
        } else {
            while (num > 0) {
                d = num % 10;
                if (d == i) {
                    ++count;
                }
                num /= 10;
            }
        }
        printf("%d ", count);
    }
    printf("\n");
    return 0;
}

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
Write a complete C++ program asking the user to enter numbers one by one. User enters...
Write a complete C++ program asking the user to enter numbers one by one. User enters only whole numbers 0 and 10. Once the user enters 0, then the program should print sum of only odd numbers entered by user prior to 0. A valid scenario given below: Enter a number: 5 Enter a number: 3 Enter a number: 2 Enter a number: 3 Enter a number: 8 Enter a number: 0 Sum of the odd numbers you entered is...
i need this code to print a number of stars depending on how much the user...
i need this code to print a number of stars depending on how much the user wants and after * it prints no stars. if the user enters a negative number it should print "error invalid number" this is my code so far: def stars(n,i): stars(n, 1) if n <= 0: return "No stars" if i <= n: print("* ", end="") stars(n, i + 1) else: print("no stars") stars(n - 1, 1) n = int(input("enter an integer")) def main(): stars()...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
In C++ Please, In the main(), below, write a program that uses a switch-statement to choose...
In C++ Please, In the main(), below, write a program that uses a switch-statement to choose between three numbers. The number, choice, should be prompted for and each choice should print out a unique statement. Also, the prompt should continue indefinitely (i.e. keep prompting until the user force-quits the program), and there should be a general catch-all if the user enters other than the three possible choices. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
I am to create three different versions of the following C program code below that implements...
I am to create three different versions of the following C program code below that implements the conversion of unsigned binary numbers into decimal (Base 2 to Base 10 conversion). Version 1: Complete the C program ”bin2dec ver1.c” that implements binary to decimal conversion. The maximum number of binary bits is 32. The program is made of the functions ”unsigned binary to decimal(const char *str)”and ”main”. The parameter ”str” passed to this function points to a C string comprising only...
C++ Part B: (String) Program Description: Write a word search program that searches an input data...
C++ Part B: (String) Program Description: Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following functions : void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount) ; (15%)...
The Problem Write a C or C++ program which performs specific operations on bits. The user...
The Problem Write a C or C++ program which performs specific operations on bits. The user will specify a value The user can then set, test, or toggle individual bits in the initial value. Input Your program will prompt the user for: An inital value in hexadecimal. This value may be up to 32 bits (eight hexadecimal digits) long. If the user-supplied value is less than eight characters in length, assume the additional digits are zero and are on the...
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to...
Write a c++ program to pull a random number between 1-100 inclusive. Ask the user to guess the number. If the random number is higher than the guess, print "Higher". If the random number is less than the guess, print "Lower". If the random number is equal to the quess, print "Correct!". Create a variable to count the number of guesses and intitialize it to zero. int guesses=0; Increase this variable by 1 every time the user enters a new...
Binary Search Tree Code in Java Write a program that prompts user to enter however many...
Binary Search Tree Code in Java Write a program that prompts user to enter however many numbers they want to add to the binary search tree. Then have the user input numbers until the tree contains that many elements. If the user enters a number that already exists in the tree, then a message should be displayed "Number is already in the tree". Once all elements are added to the tree print its contents in sorted order. Assume all user...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT