Write a C program whether the given number is Armstrong number or not.

Armstrong number:

Those Numbers which sum of its digits to power of number of its digits is equal to that numbers are known as Armstrong Numbers.

Example 1: 153 Total Digits in 153 is 3 
               1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 

Example 2: 1634 Total digits in 1634 is 4 
                1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634 

Examples of Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725
int main(){
    int num,r,sum=0,temp;

    printf("Enter a number: ");
    scanf("%d",&num);

    temp=num;
    while(num!=0)
    {
         r=num%10;
         num=num/10;
         sum=sum+(r*r*r);
    }
    if(sum==temp)
         printf("%d is an Armstrong number",temp);
    else
         printf("%d is not an Armstrong number",temp);

    return 0;
}

Out Put:
Enter a number: 153
153 is an Armstrong number

No comments:

Post a Comment