Checking whether a number is armstrong or not

The below program takes a number as an input and checks whether that number is armstrong number or not. An armstrong number is a number which is equal to the sum of individual digits to raise to the total digits present in the number. 0,1,2,3,153,370,407,1634...atc are examples of armstrong number.

Ex:
Let 153 be a number
1^3+5^3+3^3=1+125+27=153
So 153 is an armstrong number.


#include<stdio.h>
int main()
{
    int n,d,t,x,r=0,counter=0;
    printf("\nEnter your number : ");
    scanf("%d",&n);
    t=n;
    while(t!=0)
    {
        t=t/10;
        counter++;
    }
    t=n;
    while(t!=0)
    {
        x=t%10;
        r=r+pow(x,counter);
        t=t/10;
    }
  
    if(r==n)
        printf("\n%d is an armstrong number.",n);
    else
        printf("\n%d is not an armstrong number.",n);
    return 0;
}

Description:

In the above program the first while loop is used to calculate the number of digits present in the given number where as the second one is used to check the number is armstrong number or not.

Output:

case 1:

Enter your number : 370
370 is an armstrong number.

case 2:

Enter your number : 121
121 is not an armstrong number.

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Bit Stuffing Code Implementation in Java

Hackerrank Modified Kaprekar Numbers Solution