C++ Program

Object:-Write a Program to check whether given input is armstrong number or not.

#include <iostream>
using namespace std;

int main()
 int origNum, num, rem, sum = 0;
 cout << "Enter a positive  integer: ";
 cin >> origNum;

 num = origNum;
 while(num != 0)
  digit = num % 10;
  sum += digit * digit * digit;
  num /= 10;

 if(sum == origNum)
 cout << origNum << " is an Armstrong number.";
 cout << origNum << " is not an Armstrong number.";
 return 0;


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


  • In the above program, a positive integer is asked to enter by the user which is stored in the variable origNum.
  • Then, the number is copied to another variable num. This is done because we need to check the origNum at the end.
  • Inside the while loop, last digit is separated from num by the operation digit = num % 10;. This digit is cubed and added to the variable sum.
  • Now, the last digit is discarded using the statement num /= 10;.
  • In the next cycle of while loop, second last digit is separated, cubed and added to sum.
  • This continues until no digits are left in num. Now, the total sum sum is compared to the original number origNum.
  • If the numbers are equal, the entered number is an Armstrong number. If not, the number isn't an Armstrong number.

You may also like: