C++ Program to Check an Armstrong Number.

In this post, we will learn to check if a given number is Armstrong's number or not. Before checking any number we first need to understand what an Armstrong number is? Any positive n digits integer is called an Armstrong number of order n when the number can be expressed in the below format:

[abcd... = pow(a, n) + pow(b, n) + pow(c, n) + pow(d, n) + ...]

Here n = number of digit present.


Example of Armstrong Numbers.

Input: num = 153
Output: 153 is an Armstrong number.

Explanation: 
1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153

Input: num = 135
OutL 135 is not an Armstrong number.

Explanation: 1*1*1 + 3*3*3 + 5*5*5 = 1 + 27 + 125 = 153  135

Input: num = 1634
Output: 1634 is an Armstrong number.

Explanation: 
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1 + 1296 + 81 + 256 = 1634

C++ Program to check three Digit Armstrong numbers.

//C++ code to check the three-digit Armstrong number.
#include<iostream>
using namespace std;

int main(){
    
    int num, numCopy, remainder, ans = 0;
    cout<<"Enter a three digit integer: ";
    cin>>num;
    numCopy = num;

    while(numCopy != 0){
        //this remainder always contain last digit
        remainder = numCopy % 10;

        ans += remainder * remainder * remainder;

        //remove the last digit of a number.
        numCopy /= 10;
    }
    
    if(ans == num)
       cout<<num<<" is an Armstrong Number.";
    else
       cout<<num<<" is not an Armstrong Number.";   
    return 0;                    
}
Output:
Enter a three digit integer: 153
153 is an Armstrong Number.
The above program can only check three-digit Armstrong numbers but if we get any integer with more digits then the above program will fail. (alert-warning) 

C++ Program to check n Digits Armstrong number.

//C++ code to check n digit Armstrong number.
#include<iostream>
#include<math.h>
using namespace std;

int main(){
    
    int num, numCopy, remainder, ans = 0;
    cout<<"Enter an integer: ";
    cin>>num;
    numCopy = num;
    
    //variable to store a number of digits
    int n = 0;
    //counting number of digits present given num 
    while(numCopy != 0){
        numCopy /= 10;
        n++;
    }
    numCopy = num;

    while(numCopy != 0){
        //this remainder always contain the last digit
        remainder = numCopy % 10;

        ans += round(pow(remainder, n));

        //remove the last digit of number.
        numCopy /= 10;
    }
    
    if(ans == num)
       cout<<num<<" is an Armstrong Number.";
    else
       cout<<num<<" is not an Armstrong Number.";   
    return 0;                    
}
Output:
Enter a three digit integer: 1634
1634 is an Armstrong Number.

The above program can check an Armstrong number with n number of digits and to do so we first need to count the number of digits present in the user input and store it in a variable n. This calculation is done by the first while loop in the program.
 
Next, while loop will check if the number is an Armstrong number or not, and for that, we will use the math pow(a, n) function which is present inside <math.h> header file.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS