C Program for Armstrong Number - C program to check whether number is Armstrong or not
Learn C - C tutorial - Number is armstrong or not - C examples - C programs
C program to check whether number is Armstrong or not
- Armstrong Number - An Armstrong Number is a Number which is equal to it’s sum of digit’s cube. For example - 153 is an Armstrong number: here 153 = (1*1*1) + (5*5*5) + (3*3*3).
- Following program will read an integer and check whether it is Armstrong Number or Not.
- To check Armstrong number, we have to calculate sum of each digit’s cube and then compare number is equal to Sum or not.
Sample Code
#include <stdio.h>
int Power(int n, int r)
{
int k, p = 1;
for (k = 1; k <= r; k++)
p = p*n;
return p;
}
int main()
{
int a=371,sum = 0,temp,remainder,digit = 0;
temp = a;
while (temp != 0)
{
digit++;
temp = temp/10;
}
temp = a;
while (temp != 0)
{
remainder = temp%10;
sum = sum + Power(remainder, digit);
temp = temp/10;
}
if (a == sum)
printf("%d is an Armstrong number.\n", a);
else
printf("%d is not an Armstrong number.\n",a);
return 0;
}
Output
371 is an Armstrong number