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.
#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; }
Get Answers For Free
Most questions answered within 1 hours.