Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write the code for the following description and PLEASE ANSWER THE THREE

ID: 3605068 • Letter: P

Question

Please write the code for the following description and PLEASE ANSWER THE THREE QUESTIONS (a,b,c) ON THE BOTTOM. Im attaching the sample output, MAKE SURE THEY MATCH AND LOOK EXACTLY ALIKE!

SAMPLE OUTPUT:

Description: Write a C program that separates the digits of a positive integer s 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 a 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 nega- tive integer, the program should prompt the user for a positive integer. 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.

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

#include<stdbool.h>

// function to seprate the digits

void seprate(int arr[10], long long n)

{

    int i;

  

    while(n > 0)

    {

        // get the digit at 0's place

        int index = n % 10;

      

        arr[index]++;

      

        // remove the digit at 0's place

        // by dividing n by 10

        n /= 10;

    }

}

// print the table of occurencs of each digit

void printTable(int arr[])

{

    int i;

  

    printf("%-11s", "Digits:");

  

    for( i = 0 ; i < 10 ; i++ )

        printf("%4d ", i);

      

    printf(" %-11s", "Occurences:");

  

    for( i = 0 ; i < 10 ; i++ )

        printf("%4d ", arr[i]);

      

    printf(" ");

}

// return true if all the digits are different

// return false otherwise

bool ifAllDigitsDifferent(int arr[])

{

    int i;

  

    for( i = 0 ; i < 10 ; i++ )

        // if occurence of i is more than 1

        if(arr[i] > 1)

            return false;

      

    // if al the digits are different

    return true;

}

// return true if n is divisible by all the digits

// return false otherwise

bool ifDivisible(int arr[], long long n)

{

    int i;

  

    for( i = 1 ; i < 10 ; i++ )

        // if n is not divisible by i

        if(arr[i] > 0 && n % i != 0)

            return false;

          

    // n is divisible by all the digits

    return true;

  

}

void getLargestAndSecondLargest()

{

    int count = 1;

    int arr[10];

   long long i;

   

    for( i = 10000000000 ; i > 0 ; i-- )

    {

        seprate(arr, i);

       

        if( arr[0] == 0 && ifAllDigitsDifferent(arr) && ifDivisible(arr, i))

        {

            if(count == 1)

              printf("Largest Integer divisible by all of its digits : %d ",i);

            else if(count == 2)

            {

                printf("Second Largest Integer divisible by all of its digits : %d ",i);

                break;

            }

            count++;

        }

    }

}

int main()

{

    long long n;

  

    while(1)

    {

        while(1)

        {

            printf("Enter a positive integer or 0 (zero) to end: ");

            scanf("%ld", &n);

          

            if(n < 0)

                printf("Wrong input ");

            else if(n > 0)

            {

                break;

            }

            else if( n == 0)

            {

                // solution to part a, b, c

                getLargestAndSecondLargest();

               

                printf("*** Program Terminated ***");

                exit(0);

            }

          

        }

      

        int arr[10];

        int i;

      

        for( i = 0 ; i < 10 ; i++ )

            arr[i] = 0;

          

        seprate(arr, n);

      

      printTable(arr);

      

        if(arr[0] > 0)

            printf("Wrong input for the second part. Input should not contain zero.");

        else if(!ifAllDigitsDifferent(arr))

            printf("Wrong input for the second part. Input should not contain each digit more than once.");

        else

        {

            if(ifDivisible(arr, n))

                printf("%ld is divisible by its digits", n);

            else

                printf("%ld is not divisible by its digits", n);

        }

        printf(" ");

  

    }

    return 0;

}