A positive integer n is an Armstrong number (also called a narcissistic number) when it equals the sum of its digits each raised to power k, where k is the digit count of n.
Remember
k = number of digits in n
sum = d1^k + d2^k + … + dk^k
sum == n → Armstrong
Example: 153 → k = 3
1³ + 5³ + 3³ = 1 + 125 + 27 = 153
n
k
Digit powers
Armstrong?
5
1
5¹ = 5
Yes
153
3
1³+5³+3³ = 153
Yes
1634
4
1⁴+6⁴+3⁴+4⁴ = 1634
Yes
123
3
1³+2³+3³ = 36
No
3-digit Armstrong numbers
Golden interview tests — memorize this short list.
List
153, 370, 371, 407
Approach
How to Check an Armstrong Number in C#
Four clear steps — count once, then sum digit powers.
Reject n <= 0.
Count digits k (this is the exponent).
From a temp copy, peel digits with % 10 / / 10 and add digitk.
Return sum == n.
Method
Idea
Best for
Arithmetic digits
temp % 10, temp / 10, integer IPow
Interviews (default)
Range scan
Reuse IsArmstrong from low…high
“Print all in range” follow-ups
Pseudocode
Pseudocode
function IPow(base, exp):
result = 1
repeat exp times:
result = result * base
return result
function IsArmstrong(n):
if n <= 0:
return false
k = digit count of n
temp = n
sum = 0
while temp > 0:
digit = temp mod 10
sum = sum + IPow(digit, k)
temp = temp / 10
return sum == n
Try it
Live Preview
Enter any positive integer to see each digit-power term and the verdict.
Whole numbers n ≥ 1 (capped at 999999). Tap a chip or type a value.
Result
n = 153
Digits k = 3
Terms: 1^3=1, 5^3=125, 3^3=27
Sum = 153
Compare: 153 == 153
Verdict: ARMSTRONG
Trace
Worked Walkthrough — n = 153
The number has three digits, so raise every digit to the third power.
Start by checking one number, then reuse the same helper to find Armstrong numbers in a range.
Example 1 — Check One Number
Count digits, sum each digit raised to that power with an integer IPow, then compare.
C#
using System;
class Program
{
static long IPow(int value, int exp)
{
long result = 1;
for (int i = 0; i < exp; i++)
{
result *= value;
}
return result;
}
static bool IsArmstrong(int n)
{
if (n <= 0)
{
return false;
}
int power = n.ToString().Length;
int temp = n;
long total = 0;
while (temp > 0)
{
int digit = temp % 10;
total += IPow(digit, power);
temp /= 10;
}
return total == n;
}
static void Main()
{
int number = 153;
if (IsArmstrong(number))
{
Console.WriteLine(number + " is an Armstrong number.");
}
else
{
Console.WriteLine(number + " is not an Armstrong number.");
}
}
}
Output
153 is an Armstrong number.
How it works: keep the original number unchanged, extract digits from temp, and compare the integer-powered sum with n.
Example 2 — Armstrong Numbers in a Range
Reuse the same helper to list every Armstrong value from 1 to 999.
C#
using System;
class Program
{
static long IPow(int value, int exp)
{
long result = 1;
for (int i = 0; i < exp; i++)
{
result *= value;
}
return result;
}
static bool IsArmstrong(int n)
{
int power = n.ToString().Length;
int temp = n;
long total = 0;
while (temp > 0)
{
int digit = temp % 10;
total += IPow(digit, power);
temp /= 10;
}
return total == n;
}
static void Main()
{
Console.WriteLine("Armstrong numbers from 1 to 999:");
for (int value = 1; value <= 999; value++)
{
if (IsArmstrong(value))
{
Console.Write(value + " ");
}
}
Console.WriteLine();
}
}
Output
Armstrong numbers from 1 to 999:
1 2 3 4 5 6 7 8 9 153 370 371 407
How it works: the range loop calls IsArmstrong for each value. The output includes 1–9 because every single digit equals its first power.
Edge Cases & Pitfalls
Check these before calling the solution done.
Fixed ^3
Use digit count
Hard-coding cubes fails for 1-digit and 4-digit cases. Set k from n.
Destroy n
Peel a temp copy
If you mutate n in the loop, the final sum == n compare is wrong.
Math.Pow
Prefer integer power
Math.Pow returns double — use IPow for exact equality.
1…9
Single digits count
d¹ = d, so every digit 1–9 is Armstrong.
Analysis
Time and Space Complexity
Program
Time
Extra space
Check one number
O(d²), where d is the digit count
O(1)
Range 1…m
O(m · (log m)²)
O(1)
Remember
Key Takeaways
Definition: sum of each digitk equals n, with k = digit count.
Not fixed cubes: exponent comes from this n’s length every time.
Temp + IPow: peel digits from a copy; keep powers in integers.
Test set: 153 true; 123 false; 1–9 true; 370/371/407 true.
One line: count digits k, sum each digit raised to k, and check equality with n.
Frequently Asked Questions
An Armstrong number equals the sum of its digits raised to the power of its digit count. For example, 153 equals 1³ + 5³ + 3³.
Count the digits, extract each digit with % 10, add each digit raised to that count, and compare the final sum with the original number.
Yes. For every digit from 1 through 9, raising it to the first power gives the same number.
Cubes work only for three-digit values. The exponent must match the number of digits, such as 4 for the Armstrong number 1634.
For d digits, repeated integer multiplication takes O(d²) time in the worst case. The program uses O(1) extra space.