C++ Program to check whether a number is Armstrong number or not

Program to check whether a number is Armstrong number or not

Here we will discuss how to check whether a number is Armstrong number or not using C++ programming language.

Armstrong number is the number in which the sum of cubes of the individual digits of the number equals to the number itself.

To check for Armstrong number first break the number into digits, find and add the cubes of the individual digits, Compare the result with the number itself. If equal then the number is an Armstrong Number else not an Armstrong number.

For example:153

Sum of cubes of digits = 13 + 53 +33

Program to check whether a number is a Armstrong number or not

Working

  • User gives an input.
  • Input is stored in an int type variable say num.
  • input is stored an another int type variable say store.
  • A loop is started
    • In each loop cycle cube of individual digit of the number is found
    • The cube is added to sum.
    • The value of num is changed.
    • The loop will work until the value of num is greater than 0.
  • store is compared with sum
    • If both are equal then the number is a Armstrong number.
    • Otherwise the number is not a Armstrong number.

C++ Code

//C++ Program
//Armstrong numberumber or not
#include<iostream>
#include<math.h>
using namespace std;
//main Program
int main()
{
int num, digit, sum = 0;
cout << "Enter a positive integer: ";
//user input
cin >> num;
int store = num;
//find sum of cubes of individual digits
do
{
digit = num % 10;
sum = sum + pow(digit,3);
num = num / 10;
}while(num != 0);
//checking for ArmStrong number
if(sum == store)
cout << store << " is an Armstrong number.";
else
cout << store << " is not an Armstrong number.";
return 0;
}

Output

Enter a positive integer: 153
153 is an Armstrong number.

Please Login/Signup to comment