Question: Questions: When your program is complete and working correctly, use it to answer the following questions and write your answers as comments at the end
Questions:
When your program is complete and working correctly, use it to answer the following questions and write your answers as comments at the end of your program.
(a) What is the largest integer that is divisible by all of its digits?
(b) What is the second largest integer that is divisible by all of its digits?
(c) How would you find the answers to the above questions by adding code using the functions in the program?
Describe briefly.
Remember that the digits must all be different and not include zero.
Here is my working code, I'm just not sure how to answer the 3 questions using my code. Use my code to answer the questions if you need. I already know the answer to the part (a) is 9867312. I need help with b and c.
#include
#define TRUE 1
#define FALSE 0
//function prototypes
void seperateDigits(long int n);
void print(int a[],int size);
int digits_different(int a[],int size);
int divisible(int a[],int size,long int num);
/* function main */
int main()
{
//declare and initialize variables
long int input;
//print the start message
printf("Enter a positive integer or 0 (zero) to end:");
//get the user input
scanf("%d",&input);
while ( input != 0 )
{
if(input<0)
printf(" Wrong input");
else
seperateDigits(input);//call the function to separate the digits
//prompt the user
printf(" Enter a positive integer or 0 (zero) to end:");
//get the input
scanf("%d",&input);
}//end while
//print termination message
printf(" *** Program Terminated ***");
return 0;//(EXIT_SUCCESS)
}/* end main */
/* function to separate the digits of the user input */
void seperateDigits(long int n)
{
//declare an integer array of size 10 and initialize it to zero
int a[10] = {0};
//declare integer variables digit, num and initialise
int digit,num=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
num=num/10;
}//end while
print(a,10);//call print function to print the array elements accordingly
if (a[0] != 0)
{
printf(" Wrong input for the second part. ");
printf("Input should not contain zero. ");
}
else if (digits_different(a, 10) == FALSE)
{
printf(" Wrong input for the second part. ");
printf("Input should not contain each digit more than once. ");
}
else if (divisible(a, 10, n) == FALSE)
printf(" %ld is not divisible by its digits. ", n);
else
printf(" %ld is divisible by its digits ", n);
}/* end function to separate the digits */
/* function to print the array elements */
void print(int a[],int size)
{
int i;
printf(" ");//print a couple of blank lines
//print the header
printf("Digits: 0 1 2 3 4 5 6 7 8 9 ");
printf("Occurrences: ");
//for loop to print the array elements
for(i=0;i printf("%d ",a[i]); }/* end function print array */ /* digits_different function takes 2 parameters, the array of */ /* occurrences and its size and returns an integer (TRUE or FALSE) */ int digits_different(int a[],int size) { int i; for(i=0;i { if (a[i] > 1) return FALSE; } 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) */ int divisible(int a[],int size,long int num) { int i; for(i=0;i { if (a[i] == 0) continue; else if (num % i != 0) // num represents the user input return FALSE; } return TRUE;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
