Write a program to check given number is Armstrong number or not

Write a program to check given number is Armstrong number or not


// Write a program to check given number is Armstrong number or not
// Eg:-153=1^3+5^3+3^3, 370, 371, 407, 1634=1^4+6^4+3^4+4^4, 8208, 9474

#include
#include
int main(){
int length=0,i,inp,r,sum=0;
printf("Enter a number:");
scanf("%d",&inp);
if(inp>0)
{
for(i=inp;i!=0;length++) //length is number of digits
i=i/10;
for(i=inp;i!=0;i=i/10)
{
r=i%10;
sum=sum+pow(r,length);
}
if(sum==inp) // checking if Armstrong number
printf("Given number is Armstrong number\n");
else
printf("Given number is not Armstrong number\n");
}
else
printf("Given number is not Armstrong number\n");
return 0;
}

No comments:

Post a Comment