[code]#include <stdio.h> #include <stdlib.h> int Number, itr = 1, numAsd, numDes
ID: 3624111 • Letter: #
Question
[code]#include <stdio.h>#include <stdlib.h>
int Number, itr = 1, numAsd, numDes = 0, arr[3];
void sort(int arr[], int count) {
int temp;
for(int count = 1; count <= 3; count++) { //Loop to perform 3 sorting iterations on the array.
for(int 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.
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
system("pause");
}
int main() {
int temp;
printf(" Constant (495) Calculator. ");
printf("Enter a 3-Digit number: ");
scanf("%d", &Number);
if(Number <= 0) {
printf("INVALID NUMBER (it is zero or negative) ");
system("pause");
}
if(!(Number > 100 && Number < 999)) {
printf("INVALID NUMBER (NOT A 3 DIGIT NUMBER) ");
system("pause");
}
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(int 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) ");
system("pause");
}
printf("->Iteration number ");
void sort(int arr[], int count); //Sort the individual Digits in the array in ascending order
printf("The Ascending form of the number = ");
for(int 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("The Descending form of the number = ");
for(int 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]);
}
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);
system("PAUSE");
}[/code]
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int Number, itr = 1, numAsd, numDes = 0, arr[3];
void sort(int arr[], int count) {
int temp;
for(int count = 1; count <= 3; count++) { //Loop to perform 3 sorting iterations on the array.
for(int 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.
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
system("pause");
}
else Number = atoi(argv[1]);
if(Number <= 0) {
printf("INVALID NUMBER (it is zero or negative) ");
system("pause");
}
if(!(Number > 100 && Number < 999)) {
printf("INVALID NUMBER (NOT A 3 DIGIT NUMBER) ");
system("pause");
}
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(int 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) ");
system("pause");
}
printf("->Iteration number ");
void sort(int arr[], int count); //Sort the individual Digits in the array in ascending order
printf("The Ascending form of the number = ");
for(int 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("The Descending form of the number = ");
for(int 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]);
}
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);
system("PAUSE");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.