Home » C solved programs » C digits manipulation programs

C program to check whether number is Armstrong or not



Armstrong Number - An Armstrong Number is a Number which is equal to it’s sum of digit’s cube. For example - 153 is an Armstrong number: here 153 = (1*1*1) + (5*5*5) + (3*3*3).

This program will read an integer number and check whether it is Armstrong Number or Not, to check Armstrong number, we have to calculate sum of each digit’s cube and then compare number is equal to Sum or not. If Number and Sum of digit’s cube then Number will be an Armstrong Number otherwise not.

Check Armstrong Number using C program

/* C program to check whether a number is armstrong or not */
 
#include <stdio.h>
 
int main()
{
    int number, sum=0, rem=0,tempNumber;
 
    printf("Enter an integer number: ");
    scanf("%d", &number);
 
    tempNumber=number;
 
    while(tempNumber!=0)
    {
        rem=tempNumber%10;
        sum=sum + (rem*rem*rem);
        tempNumber/=10;
    }  
 
/* checking number is armstrong or not */ 
    if(sum==number)  
        printf("%d is an Armstrong number.",number);
    else
        printf("%d is not an Armstrong number.",number);
 
    return 0;
}

Using User Define Function

/* C program to check whether a number is armstrong or not */
 
#include <stdio.h>
 
/*function to check Armstrong Number*/
int isArmstrong(int num)
{
    int tempNumber=num;
    int rem,sum;
     
    /*sum of digit's cube*/
    sum=0;
    while(tempNumber!=0)
    {
        rem=tempNumber%10;
        sum=sum + (rem*rem*rem);
        tempNumber/=10;
    }  
     
    if(sum==num)
        return 1;   /*Armstrong Number*/
    else
        return 0;   /*Not an Armstrong Number*/
}
 
int main()
{
  int number;
  
  printf("Enter an integer number: ");
  scanf("%d", &number);
  
    if(isArmstrong(number))
        printf("%d is an Armstrong number.",number);
    else
        printf("%d is not an Armstrong number.",number);
  
  return 0;
}

Output

    First run:
    Enter an integer number: 153
    153 is an Armstrong number.

    Second run:
    Enter an integer number: 167
    167 is not an Armstrong number.





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.