اكتب برنامج C لإيجاد الفرق بين أكبر عدد صحيح وأصغر عدد صحيح ، والذي يتكون من 8 أرقام من 0 إلى 9
- برمجة
- برمجة سي c
- 2021-05-03
- Wassim
الأجوبة
/*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*/
#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;
}
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال