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)
String digits
n.ToString(), power = length
Quick demos
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
Cheat sheet
Goal
Pattern
Last digit
digit = temp % 10
Drop digit
temp = temp / 10
Count digits
n.ToString().Length or loop / 10
Armstrong test
sum == n (exact integer compare)
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
Digit count k = 3. Peel digits from a temp copy and add each digit3.
temp
Digit
digit3
sum
153
3
27
27
15
5
125
152
1
1
1
153
0
stop
153
Final check: 153 == 153 → Armstrong. (Order of digits in the sum does not matter.)
Code
C# Programs
Three programs: check one number, user input with the powered sum printed, and list Armstrong numbers in a range. Use View Output for sample results.
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 int IPow(int value, int exp)
{
int 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;
int 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
1. Guard and count. Reject non-positive inputs. Set power once from the digit count — that is k for every digit.
2. Peel from temp. Use temp so the original n stays intact for the final comparison.
3. Exact compare.IPow keeps everything in integers. For 153: 1+125+27 = 153.
Example 2 — User Input + Powered Sum
Interview follow-up: read n safely and print the digit-power sum beside the verdict.
C#
using System;
class Program
{
static int IPow(int value, int exp)
{
int result = 1;
for (int i = 0; i < exp; i++)
{
result *= value;
}
return result;
}
static int DigitPowerSum(int n, out int power)
{
power = n.ToString().Length;
int temp = n;
int total = 0;
while (temp > 0)
{
int digit = temp % 10;
total += IPow(digit, power);
temp /= 10;
}
return total;
}
static void Main()
{
Console.Write("Enter a positive integer: ");
if (!int.TryParse(Console.ReadLine(), out int n) || n < 1)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
int sum = DigitPowerSum(n, out int k);
Console.WriteLine("Digits k = " + k);
Console.WriteLine("Digit-power sum = " + sum);
if (sum == n)
{
Console.WriteLine(n + " is an Armstrong number.");
}
else
{
Console.WriteLine(n + " is not an Armstrong number.");
}
}
}
Output (when user enters 153)
Enter a positive integer: 153
Digits k = 3
Digit-power sum = 153
153 is an Armstrong number.
How It Works
1. Safe input.int.TryParse avoids FormatException on bad text.
2. Show the sum. Printing k and the powered sum makes wrong answers easy to debug.
3. Same rule. Armstrong means sum == n. Try 123: sum is 36, so not Armstrong.
Example 3 — Armstrong Numbers in a Range
Reuse the same helper to list every Armstrong value from 1 to 999.
C#
using System;
class Program
{
static int IPow(int value, int exp)
{
int 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;
int 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
1. One helper. The range loop never reimplements digit logic — it only calls IsArmstrong.
2. Single digits appear first. Each of 1…9 equals its own first power, so they print before 153.
3. 3-digit set. In 1…999 the only multi-digit hits are 153 370 371 407 — a classic interview checkpoint.
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.
n <= 0
Reject early
Return false for non-positive inputs in this tutorial’s convention.
1…9
Single digits count
d¹ = d, so every digit 1–9 is Armstrong.
Overflow
Watch large k
Very large digit counts can overflow int powers — fine for typical interview ranges.
Analysis
Time and Space Complexity
Program
Time
Extra space
Check one number
O(d · k) with IPow (d = k)
O(1)
Often quoted
O(log n) digit passes
O(1)
Range 1…m
O(m · log m) class work
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
A positive integer n is Armstrong when it equals the sum of its digits each raised to power k, where k is the number of digits in n. Example: 153 = 1³ + 5³ + 3³.
Yes in most programming tutorials — both mean the digit-power sum with exponent equal to the digit count equals the number.
Yes. For a one-digit value d, d¹ = d, so 1 through 9 are Armstrong numbers.
Some math definitions include 0. This tutorial checks positive integers only, so n <= 0 returns false.
Cubes only fit 3-digit numbers. The exponent must be the digit count of that specific n — 1 for 5, 3 for 153, 4 for 1634.
Math.Pow returns double and can introduce floating-point error. Prefer a small integer power helper for exact equality.
Only 153, 370, 371, and 407.
Count digits once, peel digits with % 10 and / 10 from a temp copy, sum digit^k with an integer power helper, then compare to n.
O(d²) worst case if each of d digits costs O(d) for integer power, or O(d) if power is treated as O(1) for fixed small d. Space is O(1). Often stated as O(log n) digit work.
Loop from low to high and reuse the same IsArmstrong helper for each value.
🤔
Did you know?
The only 3-digit Armstrong numbers are 153, 370, 371, and 407. A well-known 4-digit example is 1634.