C++ Program to Display Armstrong Number Between Two Intervals

To understand this example, you should have the knowledge of the following C++ programming topics:


This program asks the user to enter two integers and displays all Armstrong numbers between the given interval.

If you don't know how to check whether a number is Armstrong or not in programming then, this program may seem a little complex.

Visit this page to learn about Armstrong number and how to check it in C++ programming.

Example: Display Armstrong Number Between Intervals

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

int main() {

  int num1, num2, i, num, digit, sum, count;

  cout << "Enter first number: ";
  cin >> num1;

  cout << "Enter second number: ";
  cin >> num2;

  // swap if num1 > num2
  if (num1 > num2) {
    num1 = num1 + num2;
    num2 = num1 - num2;
    num1 = num1 - num2;
  }
  
  cout << "Armstrong numbers between " << num1 << " and " << num2 << " are: " << endl;
 
  // print Armstrong numbers from num1 to num2
  for(i = num1; i <= num2; i++) {

    // count gives the number of digits in i
    count = 0;
         
    // store value of i in num
    num = i;

    // count the number of digits in num and i
    while(num > 0) {
      ++count;
      num /= 10;
    }
 
    // initialize sum to 0
    sum = 0;

    // store i in num again    
    num = i;
        
    // get sum of power of all digits of i
    while(num > 0) {
      digit = num % 10;
      sum = sum + pow(digit, count);
      num /= 10;
    }

    // if sum is equal to i, then it is Armstrong
    if(sum == i) {
      cout << i << ", ";
    }
  }

  return 0;
}

Output

Enter first number: 1
Enter second number: 10000
Armstrong numbers between 1 and 10000 are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474,

This program prints all the Armstrong numbers within a given interval, where both the start point and endpoint of the interval are inclusive.

First, we get user input for num1 and num2 to determine our interval. If the value of num1 is greater than num2, we swap the two numbers.

Then, we find all the Armstrong numbers using a for loop:

for(i = num1; i <= num2; i++) {
  // code to print Armstrong numbers
}

Step 1: Find the Number of Digits in i

First, we need to count the number of digits contained in each number in the interval. In other words, we need to count the number of digits in i.

So, in the loop, the value of i is stored in variable num. Then, we count the number of digits in num using a while loop.

count = 0;
num = i;

// count the number of digits in num and i
while(num > 0) {
  ++count;
  num /= 10;
}

Once the while loop ends, the count variable gives us the number of digits in i.


Step 2: Find the Sum of Powers of Each Digit

We know that an Armstrong number is one in which the sum of each digit raised to the power of the number of the digits is equal to the original number itself.

So our next step is to find this sum. Similar to the previous step, we initialize the sum variable to 0 and assign the value of i to num.

sum = 0;
num = i;

Then, we use a while loop to find the sum of the powers of the digit raised to the value of the count variable.

while(num > 0) {
  digit = num % 10;
  sum = sum + pow(digit, count);
  num /= 10;
}

Notice that we have used the pow() function to calculate the power of the digit raised to the number of digits (digit ^ count). To use this function, we must import the cmath header file into our program.


Step 3: Check if sum is Equal to i

Finally, sum is compared with the original number i. If they are equal, the number is an Armstrong number, so we print it to the screen.

if(sum == i) {
  cout << i << ", ";
}
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges