C Exercises: Find the difference between the largest integer and the smallest integer created by 8 numbers from 0 to 9
C Basic Declarations and Expressions: Exercise-143 with Solution
Write a C program to find the difference between the largest integer and the smallest integer, which are created by 8 numbers from 0 to 9. The number that can be rearranged shall start with 0 as in 00135668.
Input:
Data is a sequence of 8 numbers (digits from 0 to 9).
Output:
The difference between the largest integer and the smallest integer.
Sample Solution:
C Code:
#include <stdio.h>
int main() {
int max_val, min_val, k, d, t;
printf("Input an integer created by 8 numbers (0 to 9):\n");
scanf("%d", & d);
int i, j, s[8] = {
0
};
for (i = 0; d != 0; i++) {
s[i] = d % 10;
d /= 10;
}
for (i = 0; i < 8; i++) {
for (j = 1; j + i < 8; j++) {
if (s[j - 1] < s[j]) {
t = s[j - 1];
s[j - 1] = s[j];
s[j] = t;
}
}
}
max_val = 0;
for (i = 0; i < 8; i++) {
max_val *= 10;
max_val += s[i];
}
for (i = 0; i * 2 < 8; i++) {
t = s[i];
s[i] = s[7 - i];
s[7 - i] = t;
}
min_val = 0;
for (i = 0; i < 8; i++) {
min_val *= 10;
min_val += s[i];
}
printf("\nThe difference between the largest integer and the smallest integer.\n");
printf("%d - %d = %d\n", max_val, min_val, max_val - min_val);
return 0;
}
Sample Output:
Input an integer created by 8 numbers (0 to 9): 25346879 The difference between the largest integer and the smallest integer. 98765432 - 23456789 = 75308643
Flowchart:
C programming Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C program which reads the two adjoined sides and the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus.
Next: Write a C program to create maximum number of regions obtained by drawing n given straight lines.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
Static variable inside of a function in C
The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().
The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.
The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().
Ref : https://bit.ly/3fOq7XP
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework