C# Armstrong Number Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is an Armstrong Number?

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
nkDigit powersArmstrong?
515¹ = 5Yes
15331³+5³+3³ = 153Yes
163441⁴+6⁴+3⁴+4⁴ = 1634Yes
12331³+2³+3³ = 36No

3-digit Armstrong numbers

Golden interview tests — memorize this short list.

List
153, 370, 371, 407

How to Check an Armstrong Number in C#

Four clear steps — count once, then sum digit powers.

  1. Reject n <= 0.
  2. Count digits k (this is the exponent).
  3. From a temp copy, peel digits with % 10 / / 10 and add digitk.
  4. Return sum == n.
MethodIdeaBest for
Arithmetic digitstemp % 10, temp / 10, integer IPowInterviews (default)
Range scanReuse 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

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

Worked Walkthrough — n = 153

The number has three digits, so raise every digit to the third power.

Calculation
Digits: 1, 5, 3
Powers: 1³ + 5³ + 3³
Sum:    1 + 125 + 27 = 153
Result: 153 == 153 → Armstrong number

C# Programs

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.");
        }
    }
}

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();
    }
}

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.

Time and Space Complexity

ProgramTimeExtra space
Check one numberO(d²), where d is the digit countO(1)
Range 1…mO(m · (log m)²)O(1)

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.

Next: Automorphic Number

Numbers whose square ends with the number itself.

Automorphic number tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful