Question: Description: Write a C program that separates the digits of a positive integer 10 digits long and displays the result as a table of the

Description:

Write a C program that separates the digits of a positive integer 10 digits long and displays the result as a table of the number of occurrences of each digit. For positive integers whose digits are all different and do not include zero, the program should determine whether or not the input is divisible by each of its individual digits and display an appropriate message. See the sample output. Your program must use 5 functions. One for separating the digits, one for displaying the table of the number of occurrences of each digit, one for checking to see if all the digits are different, one for checking to see if the input is divisible by each of its digits, and finally the main function for interacting with the user. You may assume that the user will always enter an integer. However, your program must check the input. If it is a negative integer, the program should prompt the user for a positive integer.

Sample output

Enter a positive integer or 0 (zero) to end: 123 Digits: 0 1 2 3 4 5 6 7 8 9 Occurrences: 0 1 1 1 0 0 0 0 0 0 123 is not divisible by its digits. Enter a positive integer or 0 (zero) to end: 1000233 Digits: 0 1 2 3 4 5 6 7 8 9 Occurrences: 3 1 1 2 0 0 0 0 0 0 Wrong input for the second part. Input should not contain zero. Enter a positive integer or 0 (zero) to end: 12223 Digits: 0 1 2 3 4 5 6 7 8 9 Occurrences: 0 1 3 1 0 0 0 0 0 0 Wrong input for the second part. Input should not contain each digit more than once. Enter a positive integer or 0 (zero) to end: 248 Digits: 0 1 2 3 4 5 6 7 8 9 Occurrences: 0 0 1 0 1 0 0 0 1 0 248 is divisible by its digits. Enter a positive integer or 0 (zero) to end: 1248 Digits: 0 1 2 3 4 5 6 7 8 9 Occurrences: 0 1 1 0 1 0 0 0 1 0 1248 is divisible by its digits. Enter a positive integer or 0 (zero) to end: 98765432 Digits: 0 1 2 3 4 5 6 7 8 9 Occurrences: 0 0 1 1 1 1 1 1 1 1 98765432 is not divisible by its digits Enter a positive integer or 0 (zero) to end: 9867312 Digits: 0 1 2 3 4 5 6 7 8 9 Occurrences: 0 1 1 1 0 0 1 1 1 1 9867312 is divisible by its digits. Enter a positive integer or 0 (zero) to end: -2 Wrong input Enter a positive integer or 0 (zero) to end: 0 *** Program Terminated *** 

PS: Here is a guideline I tried to follow when I attempted the code on my own. The teacher gave this to us.

define TRUE and FALSE include your function prototypes /* function main */ declare and initialize variables print the start message get the user input. Make sure you declare the variable for the user input as a "long int". while ( input != 0 ) if input is negative print "Wrong input" message else call the function to separate the digits. The argument to the function should be the user input prompt the user get the input end while print termination message return (EXIT_SUCCESS) /* end main */ /* function to separate the digits of the user input */ note that this is a void function and has one parameter which gets the user input declare an integer array of size 10 and initialize it to zero int a[10] = {0}; declare an integer variable called "digit" assuming the function parameter is called "n", declare another variable called "num" that is initialized to "n". while ( num > 0 ) // num has the value of user input digit = num % 10 increment the array element with index "digit", that is, ++a[digit]; divide num by 10 end while call the print function to print the array elements according to the given format. The print function has 2 parameters, the array name and its size if (a[0] != 0) print "Wrong input for the second part. " print "Input should not contain zero. " else if (digits_different(a, 10) == FALSE) print "Wrong input for the second part. " print "Input should not contain each digit more than once. " else if (divisible(a, 10, n) == FALSE) print "%ld is not divisible by its digits. ", n else print "%ld is divisible by its digits ", n /* end function to separate the digits */ /* function to print the array elements */ /* remember that the function is void and has 2 parameters */ /* the array and its size */ print a couple of blank lines print the header "Digits: 0 1 2 3 4 5 6 7 8 9 " print the word "Occurrences: " set up a for loop and print the array elements according to the given format. /* end function print array */ /* digits_different function takes 2 parameters, the array of */ /* occurrences and its size and returns an integer (TRUE or FALSE) */ set up a for loop if any element of the array is > 1 return FALSE otherwise return TRUE /* end function digits_different */ /* divisible function takes 3 parameters, the array, its size, and */ /* the user input and returns an integer (TRUE or FALSE) */ set up a for loop { if (a[i] == 0) continue; else if (num % i != 0) // num is the parameter that // represents the user input return FALSE; } return TRUE; /* end function divisible */ 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!