اكتب برنامج C لإيجاد الفرق بين أكبر عدد صحيح وأصغر عدد صحيح ، والذي يتكون من 8 أرقام من 0 إلى 9


اكتب برنامج C لإيجاد الفرق بين أكبر عدد صحيح وأصغر عدد صحيح ، والذي يتكون من 8 أرقام من 0 إلى 9. الرقم الذي يمكن إعادة ترتيبه يجب أن يبدأ بـ 0 كما في 00135668

الإدخال:

البيانات عبارة عن سلسلة من 8 أرقام (أرقام من 0 إلى 9).

الناتج :

الفرق بين أكبر عدد صحيح وأصغر عدد صحيح

مثال عن الخرج المتوقع :

Input an integer created by 8 numbers (0 to 9):
25346879

The difference between the largest integer and the smallest integer.
98765432 - 23456789 = 75308643

الأجوبة

ابحث عن مسائل برمجة سي c | C programming بالانجليزي

/*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;
}

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!