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

Please Help me to write the following program in in C Write a program that accep

ID: 3624008 • Letter: P

Question

Please Help me to write the following program in in C

Write a program that accepts a single integer as a command-line argument, validates the input and then performs process described below until it converges and reports how many iterations it took to converge and what the converged value is.

The process is as follows: Take any 3-digit number and arrange its digits in descending order and in ascending order and subtract the larger number from the smaller number. Repeat with the result until the resulting number stops changing (converges). The following is an example run of the process starting with the integer 132:
321 - 123 = 198
981 - 189 = 792
972 - 279 = 693
963 - 369 = 594
954 - 459 = 495
954 - 459 = 495 and the calculation repeats endlessly.

Before the process is applied to a number it must be checked that none of the following criteria apply to the entered number. If they apply an appropriate error message must be displayed and the program must terminate.
• it is zero or negative
• it is not a 3-digit
• it is a positive 3-digit number but all the digits are equal (e.g. 444)

Explanation / Answer

#include<stdio.h>

int Number, itr=1, numAsd, numDes=0,arr[3];

int BubbleSortArray()
{
    int count,i,j;

    for( count = 1; count<=3; count++)    //Loop to perform 3 sorting iterations on the array.
    {
        for( j=0;j<2;j++)    //Loop to perform 3 comparisons in each iteration.
        {
            if(arr[j]>arr[j+1])    //Swaps the numbers if 1st Number is larger than the 2nd.
            {
                i = arr[j];
        arr[j] = arr[j+1];
        arr[j+1]=i;
            }
        }
    }
    return 0;
}

int main()
{


int count;

printf(" Constant (495) Calculator. ");

printf("Enter a 3-Digit number: ");
scanf("%d",&Number);
        if(Number<=0)
    {
    printf( "INVALID NUMBER (it is zero or negative) " );
    return 0;
       }
    if(!(Number >100 && Number <999))
    {
printf("INVALID NUMBER (NOT A 3 DIGIT NUMBER) ");
    return 0;
    }
  
    do        //The do while loop which runs till the final number becomes = 495
    {
        numAsd=0,numDes=0;    //Initialize to 0 at the start of each iteration

        //Save the individual digits of the number to individual locations in the array
        for(count=0;count<3;++count)
        {
            arr[count]=Number%10;
            Number=Number/10;
        }
         if((arr[0]==arr[1]) && (arr[1]==arr[2]))
        {
printf( "it is a positive 3-digit number but all the digits are equal (NOT VALID) ");
        return 0;
        }
        printf("->Iteration number %d ",itr);
        BubbleSortArray(); //Sort the individual Digits in the array in ascending order

        printf("The Ascending form of the number = ");
        for(count=0;count<3;count++)    //Convert the individual digits in the array into an Ascending ordered number
        {
            numAsd=(numAsd*10)+arr[count];
            printf("%d",arr[count]);
        }

       printf(" ");
        printf("The Descending form of the number = ");
        for(count=2;count>=0;--count)    //Convert the individual digits in the array into a Descending ordered number
        {
            numDes=(numDes*10)+arr[count];
            printf("%d",arr[count]);
        }
printf(" ");

        Number=numDes-numAsd; //Subtract the Ascending ordered number from descending ordered number
        printf("Their Difference: %d - %d = %d ",numDes,numAsd,Number);
        itr++;

    }while(Number!=495);

printf("Thus constant 495 was found in %d Iterations.",itr-1);


    return 0;
}