Check Armstrong Number

ā€œCheck Armstrong Numberā€ is again basic programming exercise problem for beginners. Here, we are given a number entered by user and our task is to write a program to check whether entered number is an Armstrong number or not.

A number is said to be Armstrong number when sum of cubes of each digit of the number equals to number itself.

For example, 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153

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

using namespace std;  
int main()  
    //Scan the number  
    int num;  
    cout<<"Enter the number:";  
    //Check Armstrong number  
    int temp_num = num;  
    int check = 0;  
    while(temp_num > 0)  
        int digit = temp_num % 10;  
        check = check + (digit * digit * digit);  
        temp_num = temp_num / 10;  
    if(check == num)  
    cout<<num<<" is an Armstrong Number";  
    cout<<num<<" is not an Armstrong Number";  

Enter the number:153
153 is an Armstrong Number

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *