C Program
int power(int b, int e) { int r = 1; while (e--) r *= b; return r; } void armstrong(int low, int high) { for (int n = low; n <= high; n++) { int sum = 0, x = n, d = 0; for (int t = n; t; t /= 10) d++; for (int t = n; t; t /= 10) sum += power(t % 10, d); if (sum == n) printf("%d ", n); } }
C Output
Input: 1, 500 Output: 1 2 3 4 5 6 7 8 9 153 370 371 407
C++ Program
int power(int b, int e) { int r = 1; while (e--) r *= b; return r; } void armstrong(int low, int high) { for (int n = low; n <= high; n++) { int sum = 0, x = n, d = 0; for (int t = n; t; t /= 10) d++; for (int t = n; t; t /= 10) sum += power(t % 10, d); if (sum == n) cout << n << " "; } }
C++ Output
Input: 1, 500 Output: 1 2 3 4 5 6 7 8 9 153 370 371 407
JAVA Program
int power(int b, int e) { int r = 1; while (e-- > 0) r *= b; return r; } void armstrong(int low, int high) { for (int n = low; n <= high; n++) { int sum = 0, x = n, d = 0; for (int t = x; t > 0; t /= 10) d++; for (int t = x; t > 0; t /= 10) sum += power(t % 10, d); if (sum == n) System.out.print(n + " "); } }
JAVA Output
Input: 1, 500 Output: 1 2 3 4 5 6 7 8 9 153 370 371 407
Python Program
def armstrong(low, high): for n in range(low, high + 1): d = len(str(n)) if sum(int(i) ** d for i in str(n)) == n: print(n, end=" ")
Python Output
Input: 1, 500 Output: 1 2 3 4 5 6 7 8 9 153 370 371 407
In-Depth Explanation
Example
Let's try number 153:
It contains 3 digits.
So we calculate 1³ + 5³ + 3³ = 1 + 125 + 27 = 153
Because the result is the same as the original number, 153 is an Armstrong Number.
Now, if we're trying a range like 1 to 500, the program loops through every number, determines its digit count, raises each digit to that count, adds them up, and verifies if the sum is the same as the number.
Real-Life Analogy
Imagine every digit as a player adding to the team's total strength (the number). But every player's strength is elevated to the number of players (digits) there are. If their total strength exactly reproduces the original number, it's an equal superhero team — an unusual and powerful configuration. That's what makes an Armstrong Number "special."
Why It Matters
This exercise elegantly combines digit manipulation, power functions, and conditional logic. You learn:
How to loop through a range
Get digits out of numbers
Employ mathematics such as exponentiation
Utilize nested loops and functions effectively
You also learn to handle varying digit lengths, which is usually a necessary skill in actual problems such as encryption, digital verifications, and checksum checks.
What You'll Learn from This
You enhance your knowledge of:
Nested number manipulations
Effective loop engineering
How to compute powers manually (without built-ins)
Dealing with digit-level logic (very handy for binary, octal, or decimal conversion)
You are also exposed to:
Range-based testing
Pattern recognition
Constructing and applying helper functions (such as power)
Interview Relevance and Real Projects
Armstrong Number in a range is frequently asked in:
Freshers' interviews
College entry tests
Programming tests
It tests:
Loop control
Mathematical skills
Ability to handle multi-digit logic
Real-world applications include:
Validation algorithms (digital lock systems)
Checksum and ID pattern recognition
Math education software
Game logic where power/points system follows this logic
SEO-Optimized Explanation
Armstrong Number in a range is one of the most common programming exercises in C, C++, Java, and Python that deals with verifying which numbers in a specified range fulfill an interesting mathematical property: the number is equal to the sum of its digits powered by the count of digits. This reasoning is applied extensively to construct programming building blocks for digit-based operations, loops, conditionals, and power functions. It's perfect for coding interviews, competitive programming, and training for working with mathematical patterns within a range. Armstrong numbers mastery enables programmers to improve algorithmic thinking and learn to decompose and examine numbers programmatically.
Social Plugin