C program for Armstrong Number
C program for armstrong number : The following C program checks whether input number is armstrong or not.
Example of Armstrong number: 153 = 1*1*1 + 5*5*5 + 3*3*3
Example of Armstrong number: 153 = 1*1*1 + 5*5*5 + 3*3*3
Algorithm to check armstrong number
- Accept a number.
- Count the number of digits in a number.
- Calculate the digit raised to number of digits, that is power(digit,number of digits) for each digit.
- Sum up all these calculations.
- If the number obtained by summing up is equal to the original input number then it is a armstrong number.
C Program to check Armstrong number
Here is the source code of C program for armstrong number/* C program to check whether the number is Armstrong number or not */
#include<stdio.h>
#include<math.h>
int main()
{
int temp,count,original,num,sum=0,r;
printf("\n Enter any number:- ");
scanf("%d",&num);
temp=num;
while(num>0)
{
r=num%10;
num/=10;
count++;
}
original=temp;
while(temp>0)
{
r=temp%10;
sum=sum+pow(r,count);
temp/=10;
}
if (original==sum)
{
printf("\n %d is a Armstrong number \n \n",original);
}
else
{
printf("\n %d is not a Armstrong number \n \n",original);
}
return 0;
}
/* Output of above code:-
Enter any number:- 153
153 is a Armstrong number
*/