Armstrong number in C

  • برمجة سي c

Armstrong number in C

Armstrong number C program to check whether a number is an Armstrong number or not, it's a number that is equal to the sum of digits raise to the power total number of digits in the number. Some Armstrong numbers are: 0, 1, 2, 3, 153, 370, 407, 1634, 8208, etc. We will consider base ten numbers in our program. The algorithm to do this is: First, we calculate the number of digits in our program and then compute the sum of individual digits raise to the power number of digits. If this sum equals the input number, then the number is an Armstrong number otherwise not

الأجوبة

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

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

  printf("Input an integer\n");
  scanf("%d", &n);

  t = n;
  // Count number of digits
  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)
    printf("%d is an Armstrong number.\n", n);
  else
    printf("%d isn't an Armstrong number.\n", n);

  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 an integer

9876543

9876543 isn't an Armstrong number.

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

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

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