C++ Program to Check Armstrong Number (3 Ways)

In this program, we will learn how to check whether a number is Armstrong Number or not using C++ programming language.

What is Armstrong Number?

It is a number whose sum of the nth power of digits is equal to the number itself. Where n is called the order, which is equal to the number of digits present in the number.

For example:-
Let’s take the number 371. Let’s see

Order of number 371 (n) = 3
371 = 33 + 73 + 13

Let’s take one more example of number 1634

Order of number 1634 (n) = 4
1634 = 14 + 64 + 34 + 44

Now you get the understanding of the Armstrong number, so, let’s implement in a C++ program.
I will be discussing three ways to write code for it.

  1. By taking the number as int datatype.
  2. By taking the number as string datatype.
  3. And by using recursion.

C++ Programming Code to Check Armstrong Number

By taking the number as int datatype

We have made a getDigitCount() function to get the order of the number. And passed it to the pow() function of math.h library to get the power of the digit raised to the order.

We will be using the while loop to get the individual digits of the number.

Code:-

#include<iostream>
#include<math.h>

using namespace std;

int getDigitCount(long long n) { 
    int count = 0; 
    while (n != 0) { 
        n = n / 10; 
        ++count; 
    } 
    return count; 
}

int main (){
    int num, temp, rem, sum = 0;

    cout << "Enter a number: ";
    cin >> num;

    int order = getDigitCount(num);

    temp = num;
    while (temp != 0){
        rem = temp % 10;
        sum = sum + pow(rem, order);
        temp = temp / 10;
    }

    if (sum == num)
        cout << num << " is an Armstrong number." << endl;
    else
        cout << num << " is not an Armstrong number." << endl;
        
    return 0;
}

Output:-

Enter a number: 371
371 is an Armstrong number.

By taking the Number as String datatype

In this program, We don’t require to have a function to get the order of the number. We can just use the size() in-build function to get the length of the number (order).

We have used the for loop to get the individual digits of the number.

Code:-

#include<iostream>
#include<math.h>

using namespace std;

int main (){
    string num;
    int d, sum = 0, temp = 0;
    cout << "Enter a number: ";
    cin >> num;

    int order = num.size();

    for(int i=0;i<num.size();i++){
        d = (num[i] - '0');
        sum += pow(d, order);
        temp = (temp * 10) + d;
    }

    if (sum == temp)
        cout << num << " is an Armstrong number." << endl;
    else
        cout << num << " is not an Armstrong number." << endl;
        
    return 0;
}

Output:-

Enter a number: 371
371 is an Armstrong number.

By using Recursion

In this program, I have again used the getDigitCount() method to get the order of the number.

And also a recursion function getSum() to get the sum of the power of the digit raised to the order.

Code:-

#include<iostream>
#include<math.h>

using namespace std;

int getDigitCount(long long n) { 
    int count = 0; 
    while (n != 0) { 
        n = n / 10; 
        ++count; 
    } 
    return count; 
}

int getSum(int num) {
    static int order = getDigitCount(num);
    if(num == 0){
        return 0;
    }else{
        return pow(num%10, order) + getSum(num/10);
    }
}


int main (){
    int num, sum;

    cout << "Enter a number: ";
    cin >> num;

    sum = getSum(num);

    if (sum == num)
        cout << num << " is an Armstrong number." << endl;
    else
        cout << num << " is not an Armstrong number." << endl;
        
    return 0;
}

Output:-

Enter a number: 371
371 is an Armstrong number.

You can learn about many other C++ Programs Here.

Best Books for learning C++ programming language with Data Structure and Algorithms.

The following two tabs change content below.

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...