Program to check if a number is Armstrong or not

Definition (Armstrong Number): An n-digit number equal to the sum of the nth powers of its digits.
Example:
153 = 13 + 53 + 33
1634 = 14 + 64 + 34 + 44

#include<stdio.h>
#include<math.h>
int main(){
    long int num =153, count = 0, i, temp1, temp2, temp3 = 0;
    clrscr();
    printf("Enter the number: ");
    scanf("%ld",&num);

    if(num <= 0){
        printf("\nEnter the number greater than 0");
        return 0;
    }

    temp1 = temp2 = num;

    while(temp1!=0){
        temp1=temp1/10;
        count++;
    }

    for(i=0;i < count; i++)
    { 
        temp3 = temp3 + pow((temp2%10),count);
        temp2 = temp2/10; 
    }

    if(num == temp3)
    {
        printf("\n %d is an armstrong number",num);
    }
    else
    {
        printf("\n %d is an armstrong number",num);
    }

    return 0;
}

Output:

Enter the number: 92727

92727 is an armstrong number

Have any questions regarding this program? Post a comment.

You may also like...

4 Responses

  1. Sam says:

    Nice share

  2. it can be also written simpler than this. without using and so confusion don’t arrives.

  3. Anonymous says:

    Can u plz tell us different math.h functions you have used.??

  1. March 2, 2014

    […] Program to find whether a number is Armstrong or not […]

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet