C++ Program to Check for Armstrong Number

  • Write a program in C++ to check armstrong number.

C++ program to check armstrong number

#include <iostream>

using namespace std;
 
int main(){
    int number, sum = 0, lastDigit, temp;
    cout << "Enter a Number\n";
    cin >> number;
    temp = number;
    // Calculate sum of cube of every digit
    while(temp != 0){
        lastDigit = temp%10;
        sum = sum + (lastDigit*lastDigit*lastDigit);
        temp = temp/10;
    }
     
    if(sum == number){
        cout << number <<" is Armstrong Number";
    } else {
        cout << number <<" is not an Armstrong Number";
    }
    
    return 0;
}
Output
Enter a Number
153
153 is Armstrong Number
Enter a Number
120
120 is not an Armstrong Number