C program to Check whether a Number is Armstrong or Not

A number is Armstrong, if sum of it’s digit raised to power of total number of digits in that number is equal to the original number .
Example.
consider a number 153, it has 3 digits, where  13+53+33=153.
Hence 153 is an Armstrong number.

% is the remainder after division (modulo division) arithmetic operator in C.
11 % 5 = 1 (11 / 5 = 2 with remainder 1)

```// C program to find Armstrong number

#include <stdio.h>

int power(int x, unsigned int y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}

int count(int x)
{
int n = 0;
while (x)
{
n++;
x = x / 10;
}
return n;
}

int isArmstrong(int x)
{
int n = count(x);
int temp = x, sum = 0;
while (temp)
{
int r = temp % 10;
sum += power(r, n);
temp = temp / 10;
}

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

int main()
{
int num;
printf("\nEnter the number=");
scanf("%d", &num);
if (isArmstrong(num) == 1)
printf("%d is an Armstrong number\n", num);
else
printf("%d is an not Armstrong number\n", num);
return 0;
}

```
 \$  gcc armstrong.c \$ ./a.outEnter the number=5 5 is an Armstrong number \$ ./a.outEnter the number=153 153 is an Armstrong number \$ ./a.outEnter the number=1 1 is an Armstrong number \$ ./a.out Enter the number=99 99 is an not Armstrong number \$ ./a.out Enter the number=1634 1634 is an Armstrong number \$

If you like the post C program to Check whether a Number is Armstrong or Not, please share your feedback!

also see

Previous articleC Program to Check Whether a Number is Palindrome or Not
Next articleQueue Implementation using Array