- Write a C program to check whether a number is Armstrong number or not.

A number is called an **Armstrong number** if the sum of cubes of every digit of a number is equal to the number itself.

*For Example*

407 is an Armstrong number

407 = 4*4*4 + 0*0*0 + 7*7*7

121 is not an Armstrong number

121 is not equal to 1*1*1 + 2*2*2 + 1*1*1

**Algorithm to check for Armstrong number**

- Take a number as input from user and store it in an integer variable(Let's call it inputNumber).
- Find the cubic sum of digits of inputNumber, and store it in sum variable.
- Compare inputNumber and sum.
- If both are equal then input number is Armstrong number otherwise not an Armstrong number..

## C program to check a number is Armstrong number or not

This program first takes a number as input from user using scanf function and stores it in variable 'number'. It makes a copy of number in variable 'temp'. Then using a while loop, it calculates the cube of every digit of temp(loop will terminate when temp becomes zero) and stores in a 'sum' variable. If sum is equal to number then it is an Armstrong number otherwise not an Armstrong number.

/* * C Program to check whether a number is armstrong number or not */ #include <stdio.h> #include <conio.h> int main(){ int number, sum = 0, lastDigit, temp; printf("Enter a number : "); scanf("%d", &number); temp = number; while(temp != 0){ lastDigit = temp%10; sum = sum + (lastDigit*lastDigit*lastDigit); temp = temp/10; } if(sum == number){ printf("%d is Armstrong Number \n", number); } else { printf("%d is not an Armstrong Number \n", number); } getch(); return 0; }

**Program Output**

Enter a number : 153 153 is Armstrong Number

Enter a number : 120 120 is not an Armstrong Number

## C Program to check whether a number is Armstrong number or not using function

This program uses a user defined function getCubicSumOfDigits that returns the cubic sum of all digits of a number.

/* * C Program to check whether a number is armstrong number or not */ #include <stdio.h> #include <conio.h> int getCubicSumOfDigits(int number); int main(){ int number, sum; printf("Enter a number \n"); scanf("%d", &number); sum = getCubicSumOfDigits(number); if(sum == number){ printf("%d is Armstrong Number \n", number); } else { printf("%d is not an Armstrong Number \n", number); } getch(); return 0; } /* * Funtion to calculate the sum of cubes of digits of a number * getCubicSumOfDigits(123) = 1*1*1 + 2*2*2 + 3*3*3; */ int getCubicSumOfDigits(int number){ int lastDigit, sum = 0; while(number != 0){ lastDigit = number%10; sum = sum + lastDigit*lastDigit*lastDigit; number = number/10; } return sum; }

**Program Output**

Enter a number 407 407 is Armstrong Number

**Related Topics**