# 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 n^{th} 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 = 3^{3} + 7^{3} + 1^{3}

Let’s take one more example of number 1634

Order of number 1634 (n) = 4

1634 = 1^{4} + 6^{4} + 3^{4} + 4^{4}

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.

- By taking the number as int datatype.
- By taking the number as string datatype.
- 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:-

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:-

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:-

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.

#### Amit Rawat

#### Latest posts by Amit Rawat (see all)

- Python Program to Print the Fibonacci Sequence (2 ways) - April 7, 2020
- Python Program to Display or Print Prime Numbers Between a Range or an Interval - June 18, 2019
- Python Program To Print Pascal’s Triangle (2 Ways) - June 17, 2019