# Algorithm And Program For Armstrong Number

## Algorithm And Program For Armstrong Number

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For e.g. 153 (13 + 53 + 33 = 153) is an Armstrong number. All single digit numbers are armstrong numbers. Two digit numbers are never armstrong numbers. It is easy to find if a three digit number is armstrong or not. To check for other numbers we have to use pow function. It is an built-in function. Let’s see Algorithm And Program For Armstrong Number.

## 1. Algorithm

``````1.START
2.PRINT “ENTER A NUMBER:”
4.num = temp
5.REPEAT STEPS   WHILE num / 10 == 0
rem = num % 10
sum = sum + (rem*rem*rem)
num = num/10
[END OF WHILE]
6.IF temp == sum THEN
PRINT “GIVEN NUMBER IS ARMSTRONG NUMBER”
7.ELSE
PRINT “GIVEN NUMBER IS NOT AN ARMSTRONG NUMBER”
[END OF IF]
8.STOP
``````

## 2. Code

### C program

``````//Armstrong number program in C
#include<stdio.h>

int main()
{
int num, rem, sum = 0, temp;
printf("\nEnter the number=");
scanf("%d",&num);
temp = num;
while(num>0)
{
rem = num%10;
sum = sum+(rem*rem*rem);
num = num/10;
}
if(temp == sum)
printf("\n%d is an Armstrong  number",temp);
else
printf("\n%d is not an armstrong number",temp);

return 0;
} ``````

### C++ program

``````#include <iostream>

using namespace std;

int main() {
int num, temp, rem, sum = 0;
cout << "\nEnter a three-digit integer: ";
cin >> num;
temp = num;

while (num != 0)
{
rem = num % 10;
sum =sum + (rem * rem * rem);
num = num/10;
}

if (sum == temp)
cout << temp << " is an Armstrong number.";
else
cout << temp << " is not an Armstrong number.";

return 0;
}``````

## 4. Program parts

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. conio is a header file which is use to include commands like clrscr(),getch() in the code.
4. .h is an extension for header file.
5. int is data type. It indicates that the program returns a value.
6. main() is main function that indicates the compiler that the user written programs starts from here.
7. num, rem, sum, temp are variable names of integer data type.
8. while is looping structure in C/C++. It repeats the code inside it untill a specific condition is satisfied.
9. print/cout is the keyword use to print a message.
10. return statement is use to return a value. If any error occurs the program will return 0.

## 5. Explanation

The program will accept a number. Then it will store the accepted number in temp variable. Now the program will enter while loop. In while loop it will divide the accepted number by 10 and the remainder will be stored in rem. sum is used to store the sum of cube of the current remainder and cube of previous remainder. Then the num will divide itself with 10 and store the quotient in num only. The loop will execute until the condition is satisfied. Once the condition is not satisfied it will come out of the loop. It will now check if the original number (stored in temp) is as same as sum. If it is same then it will print “Given number is an Armstrong number” else it will print “Given number is not an Armstrong number”.

## Conclusion

So these were algorithm and program for Armstrong number. there are many other possible ways to calculate Armstrong number. If you know any of them do tell us in the comment section below. Hope you liked it. If yes then please do share it with your friends and programming buddies. Also tell us what other programs you would like us to post.

For more such content follow us on instagram. Visit again!

View All Posts