Check Armstrong in Range in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
#include <math.h>
int main() {
    int low, high, num, sum, temp, digits;
    scanf("%d %d", &low, &high);
    for(num = low; num <= high; num++) {
        temp = num;
        digits = 0;
        while(temp) { digits++; temp /= 10; }
        temp = num;
        sum = 0;
        while(temp) {
            sum += pow(temp % 10, digits);
            temp /= 10;
        }
        if(sum == num) printf("%d ", num);
    }
    return 0;
}

C Output

Input:
100 500

Output:
153 370 371 407


C++ Program

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int low, high;
    cin >> low >> high;
    for(int num = low; num <= high; num++) {
        int temp = num, digits = 0, sum = 0;
        while(temp) { digits++; temp /= 10; }
        temp = num;
        while(temp) {
            sum += pow(temp % 10, digits);
            temp /= 10;
        }
        if(sum == num) cout << num << " ";
    }
}

C++ Output

Input:
1 1000

Output:
1 153 370 371 407


JAVA Program

import java.util.*;
class ArmstrongRange {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int low = sc.nextInt(), high = sc.nextInt();
        for(int num = low; num <= high; num++) {
            int temp = num, digits = 0, sum = 0;
            while(temp > 0) { digits++; temp /= 10; }
            temp = num;
            while(temp > 0) {
                sum += Math.pow(temp % 10, digits);
                temp /= 10;
            }
            if(sum == num) System.out.print(num + " ");
        }
    }
}

JAVA Output

Input:
150 500

Output:
153 370 371 407

Python Program

low, high = map(int, input().split())
for num in range(low, high+1):
    digits = len(str(num))
    sum_ = sum(int(d)**digits for d in str(num))
    if sum_ == num:
        print(num, end=" ")

Python Output

Input:
10 200

Output:
153


In-Depth Explanation
Example
An Armstrong number is one where the sum of its digits raised to the power of the number of digits equals the number itself. For instance, 153 has three digits. If we calculate 13+53+331^3 + 5^3 + 3^3, we get 1+125+27=1531 + 125 + 27 = 153. This matches the original number, so 153 is an Armstrong number. When we check within a range, we apply the same rule to each number.

Real-Life Analogy
Imagine each digit as a member of a team offering their "power" to the team. In an Armstrong number, if each member offers their best (their digit to the power of the number of members), their contributions add up in the exact amount of the combined score of the team — their own number. If the perfect match exists, they are an Armstrong team.

Why It Matters
Armstrong numbers are important to learn about digit manipulation, loops, and operations in mathematics such as exponentiation. These principles form the foundation for a higher-level problem, ranging from digital security checks to pattern recognition and cryptography.

Learning Insights
As you solve it, you learn to decompose a number into digits, compute a property for each digit, and reassemble the result to compare with the original. It exercises your handling of integer operations, loops, and logic flow in programming. It's also one of the best brute-force search examples applied on a bounded range. 

Interview and Real-World Use
This type of question tends to turn up during programming interviews to challenge knowledge of loops, processing of numbers, and conditional statements. Outside of interviews, principles such as these find application in checksum verification (credit cards, barcodes), data integrity checks, and encoding systems, where mathematical relationships between the digits are significant.

SEO-Optimized Closing
Verification of Armstrong numbers within a range is a traditional programming exercise for beginners that enhances proficiency in loops, mathematical operations, and manipulating numbers. Comprehending this reasoning aids in solving actual applications of digit manipulation and power computation. Preparing for coding interviews or establishing a solid programming background by learning how to calculate Armstrong numbers within a certain range enhances problem-solving and algorithmic mindsets. Fully grasping such matters readies one for handling more advanced problems in competitive programming and software development.