Table of contents
1.
Introduction
2.
What is Armstrong Number in C++?
3.
Examples of Armstrong Number in C++?
3.1.
Example 1:
3.2.
Example 2:
3.3.
Example 3:
4.
Solution Approach 
4.1.
Steps of algorithm
4.2.
Implementation in C++
4.3.
C++
4.3.1.
Complexity Analysis
5.
Finding Armstrong Number in C++ Using Strings
5.1.
C++
6.
Finding All Armstrong Numbers within a Range
6.1.
C++
7.
Frequently Asked Questions
7.1.
Is 123 an Armstrong number?
7.2.
Is 54748 Armstrong a number?
7.3.
How 1634 is an Armstrong number?
7.4.
What is the Armstrong number from 1 to 100?
8.
Conclusion
Last Updated: Jan 2, 2025
Easy

Check Armstrong Number in C++

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Armstrong numbers, also called narcissistic numbers, have a cool property that's easy to understand once you break it down. Imagine you have a number. Now, if you take each digit of that number, raise it to the power of how many digits there are in total, and add them together, something interesting might happen. If the result of this process equals the original number, then congratulations! You've found an Armstrong number.

Check Armstrong Number in C++

What is Armstrong Number in C++?

An Armstrong number is the sum of its digits, each raised to the power of the digits' number, i.e., if we have a three-digit number, then the sum of the cube of all the digits must equal the number itself. 0, 1, 153, 370 are some examples of Armstrong numbers. 

We will first discuss sample examples to understand better. 

Examples of Armstrong Number in C++?

Example 1:

Input: 370
Output: 370 is an Armstrong number  
Explanation: 3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370
Since the sum of the cube of all digits is equal to the number itself, hence it is Armstrong number.

Example 2:

Input: 152 
Output: 152 is not an Armstrong number
Explanation: 1^3 + 5^3 + 2^3 = 1 + 125 + 8 = 134
Since the sum of the cube of all digits is not equal to the number itself, hence it is not an Armstrong number. 

Example 3:

Input: 1634
Output: 1634 is not an Armstrong number
Explanation: 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634
Since the sum of all digits raised to a number of digits is equal to the number itself, hence it is an Armstrong number. 

Solution Approach 

The solution to this problem is very trivial, we extract every digit of number n, and take a temporary number, let's say, temp, to store the sum of all digits raised to a number of digits in a given number n, then, at last, we will compare it with the number n, if both are equal then result is an Armstrong number; otherwise, it is not an Armstrong number. 

Steps of algorithm

  1. Make a variable temp, and initialize it with n, so that we can compare it with sum to check whether the given number is Armstrong or not. 
  2. Now extract each digit of n by taking the remainder with 10, continuously dividing it with n until n > 0, and storing the result in the sum variable. 
  3. At last, check whether temp == sum or not; if yes then print Armstrong number otherwise, not an Armstrong number. 

Implementation in C++

  • C++

C++

// c++ program to check whether the given number is armstrong or not
#include<bits/stdc++.h>
using namespace std;
int isArmstrongNumber(int n){
   // Calculating the number of digits
   int digit = log10(n) + 1;
  
   // taking temporary variable to check
   // in last whether the given number is armstrong or not
   int temp = n;
   int sum = 0;
  
   while(n>0){
       // getting the digit of a number n
       int lastDigit = n % 10;
       n/=10;
       // adding the digit raised to number of digits in the sum variable
       sum += pow(lastDigit, digit);
   }
   
   // return true if sum and temp are equal
   // otherwise return false
   return temp == sum;
}
int main(){
   int n = 1634;
   if(isArmstrongNumber(n))
       cout << n << " is an armstrong number" << endl;
   else
       cout << n << " is not an armstrong number" << endl;
}
You can also try this code with Online C++ Compiler
Run Code

Output::

1634 is an Armstrong number 

 

Try and compile with online c++ compiler.

Complexity Analysis

Time Complexity: O(LogN)

Explanation: While loop n>0, will run at most logN time, hence time complexity is O(logN). 

Space ComplexityO(1)

Explanation: We are not using an explicit space, so time complexity is O(1). 

Finding Armstrong Number in C++ Using Strings

In this approach, we convert the number to a string to easily access its individual digits. Then, we loop through each digit, raise it to the power of the total number of digits, and sum them up. If the result equals the original number, it's an Armstrong number. 

Implementation:

  • C++

C++

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

bool isArmstrong(int num) {
string numStr = to_string(num);
int n = numStr.size();
int sum = 0;
for (char digit : numStr) {
sum += pow(digit - '0', n);
}
return sum == num;
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isArmstrong(num)) {
cout << num << " is an Armstrong number.";
} else {
cout << num << " is not an Armstrong number.";
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Finding All Armstrong Numbers within a Range

To find Armstrong numbers within a range, iterate through the range and check each number using the same logic as above. 

Implementation:

  • C++

C++

#include <iostream>
#include <cmath>
using namespace std;

bool isArmstrong(int num) {
int originalNum = num;
int n = to_string(num).size();
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += pow(digit, n);
num /= 10;
}
return sum == originalNum;
}

int main() {
int start, end;
cout << "Enter the range (start and end): ";
cin >> start >> end;
cout << "Armstrong numbers within the range are:" << endl;
for (int num = start; num <= end; ++num) {
if (isArmstrong(num)) {
cout << num << endl;
}
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Frequently Asked Questions

Is 123 an Armstrong number?

No, 123 is not an Armstrong number because 1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36, which is not equal to 123.

Is 54748 Armstrong a number?

Yes, 54748 is an Armstrong number because 5^5 + 4^5 + 7^5 + 4^5 + 8^5 = 54748, making it a valid Armstrong number.

How 1634 is an Armstrong number?

1634 is an Armstrong number because 1^4 + 6^4 + 3^4 + 4^4 = 1634, meaning the sum of its digits raised to the fourth power equals the number itself.

What is the Armstrong number from 1 to 100?

Armstrong numbers from 1 to 100 are numbers equal to the sum of their digits raised to the power of the number of digits. For this range, the Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, and 153.

Conclusion

In this article, we discussed the problem in which we are given a number n and we need to check whether the given number is an Armstrong number or not. We hope you understand the problem and solution properly. Now you can do more similar questions. 

Live masterclass