C program to print Armstrong numbers

  • برمجة سي c

C program to print Armstrong numbers

 

C program to print Armstrong numbers, in the program, a user inputs two integers, and we print all Armstrong numbers between the integers. Using a for loop, we check numbers in this range. In the loop, we call function check_armstrong, which returns '1' if a number is Armstrong and '0' otherwise.

الأجوبة

#include <stdio.h>
int check_armstrong(int);
int power(int, int);

int main ()
{
  int c, a, b;

  printf("Input two integers\n");
  scanf("%d%d", &a, &b);

  for (c = a; c <= b; c++)
    if (check_armstrong(c) == 1)
      printf("%d\n", c);

  return 0;
}

int check_armstrong(int n) {
  long long sum = 0, t;
  int remainder, digits = 0;

  t = n;

  while (t != 0) {
    digits++;
    t = t/10;
  }

  t = n;

  while (t != 0) {
    remainder = t%10;
    sum = sum + power(remainder, digits);
    t = t/10;
  }

  if (n == sum)
    return 1;
  else
    return 0;
}

int power(int n, int r) {
  int c, p = 1;

  for (c = 1; c <= r; c++)
    p = p*n;

  return p;
}

output:

Input two integers

2

3

2

3

هل كان المحتوى مفيد؟

القوائم الدراسية التي ينتمي لها السؤال

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...