C Program to Check Armstrong (Narcissistic) Number
A number is said to be Armstrong or Narcissistic Number if it is equal to the sum of its own digits raised to the power of the number of digits in a given number.
For example number 153 is Armstrong number because: 13 + 53 + 33 = 153
Similary, 1634 is also Armstrong number i.e. 14+64+34+44 = 1634
Single digits number 1 to 9 are also Armstrong numbers.
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int number, original, rem, sum=0, digit=0;
clrscr();
printf("Enter number: ");
scanf("%d", &number);
original = number;
/* Counting number of digit in a given number */
while(number!=0)
{
digit++;
number = number/10;
}
/* After execution above loop number becomes 0
So copying original number to variable number */
number = original;
/* Finding sum */
while(number != 0)
{
rem = number%10;
sum = sum + pow(rem, digit);
number = number/10;
}
/* Making decision */
if(sum == original)
{
printf("%d is ARMSTRONG.", original);
}
else
{
printf("%d is NOT ARMSTRONG.", original);
}
getch();
return(0);
}
Output of the above program :
Run 1: ---------- Enter number: 153 ↲ 153 is ARMSTRONG. Run 2: ---------- Enter number: 1634 ↲ 1634 is ARMSTRONG. Run 3: ---------- Enter number: 1234 ↲ 1234 is NOT ARMSTRONG. Note: ↲ indicates enter is pressed.