Program to Find Armstrong Numbers in a Given Range in C++

Armstrong Number in a Given Range

An Armstrong number (or narcissistic number) is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 and 9474 are Armstrong numbers.

We will explore a method to find Armstrong numbers within a given range using C++ programming.

Method: Using a for Loop

We iterate through the given range and check each number for the Armstrong condition.

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

bool isArmstrong(int num) {
    int originalNum = num, sum = 0, digit, n = 0;
    
    // Count the number of digits
    int temp = num;
    while (temp != 0) {
        temp /= 10;
        n++;
    }
    
    temp = num;
    
    // Calculate the sum of digits raised to the power of n
    while (temp != 0) {
        digit = temp % 10;
        sum += pow(digit, n);
        temp /= 10;
    }
    
    return (sum == num);
}

int main() {
    int start, end;
    cout << "Enter the range (start end): ";
    cin >> start >> end;
    
    cout << "Armstrong numbers between " << start << " and " << end << " are: ";
    for (int i = start; i <= end; i++) {
        if (isArmstrong(i)) {
            cout << i << " ";
        }
    }
    
    return 0;
}
            

Output:

Enter the range (start end): 1 1000
Armstrong numbers between 1 and 1000 are: 1 153 370 371 407
Easy aceess next quctions
Getting Started

Positive or Negative number: C C++ Java Python

Even or Odd number: C C++ Java Python

Sum of First N Natural numbers: C C++ Java Python

Sum of N natural numbers: C C++ Java Python

Sum of numbers in a given range: C C++ Java Python

Greatest of two numbers: C C++ Java Python

Greatest of the Three numbers: C C++ Java Python

Leap year or not: C C++ Java Python

Prime number: C C++ Java Python

Prime number within a given range: C C++ Java Python

Sum of digits of a number: C C++ Java Python

Reverse of a number: C C++ Java Python

Palindrome number: C C++ Java Python

Armstrong number: C C++ Java Python

Armstrong number in a given range: C C++ Java Python

Harshad number: C C++ Java Python

Abundant number: C C++ Java Python

Friendly pair: C C++ Java Python