A number is called an Armstrong number if the sum of its own digits each raised to the power of the number of digits.
We can write in its generalized form as –
abcd...= an+bn+cn+dn+...
It is named after Michael F. Armstrong the Armstrong number is known with some other names –
- Narcissistic number
- pluperfect digital invariant (PPDI)
- Plus perfect number
This article explains how to check if a number is Armstrong or not using a C program.
Example of Armstrong number
A few examples of Armstrong number are given below.
153 is an Armstrong number that has 3 digits.
To verify this calculate the sum of the cube of each digit if should be equal to the given number i.e. 153
13+53+33 = 153
Similarly, 1634 is a four-digit Armstrong number you can verify this also by calculating the sum of its own digits each raised to the power 4 it should be equal to the given number i.e. 1634
14+64+34+44 = 1634
Some other examples are 1, 2, 3, 4, 5, 6, 7, 8, 9, 370, 371, 407, etc.
C program to check for an Armstrong number
You can enter an n digit number to check if it is an Armstrong number or not. Now see the given C program.
//C program to check an n digit armstrong number
#include<stdio.h>
#include<math.h>
int main() {
int num, original, rem, sum = 0, n = 0;
printf("Enter a Number: ");
scanf("%d", & num);
original = num;
while (original != 0) {
original /= 10;
++n;
}
original = num;
while (original != 0) {
rem = original % 10;
sum += pow(rem, n);
original /= 10;
}
if (sum == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Save this program with the .c
extension and then execute it. For more help, you can read how to compile and run c program in Linux.
Conclusion
In this article, you learned how to check if a number is an Armstrong number or not using a C program. Now if you have a query then write us in the comments below.