C++ Program to Check for Armstrong Number

Here is a C++ program 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

Recommended Posts
C++ Program to Check Prime Number
C++ Program to check Whether a Number is Palindrome or Not
C++ Program to Display Armstrong Number Between Two Intervals
C++ Program to Find GCD or HCF of Two Numbers Using Recursion
C++ Program to Find Power of Number using Recursion
C++ Program to Find Factorial of a Number
C++ Program to Convert Temperature from Celsius to Fahrenheit
C++ Program to Multiply Two Matrices
C++ Program to Copy String Without Using strcpy
All C++ Programs