C# Armstrong Number Program

Beginner
7 min read
Updated: Sep 2026
3 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)
String digitsn.ToString(), power = lengthQuick demos
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

Cheat sheet

GoalPattern
Last digitdigit = temp % 10
Drop digittemp = temp / 10
Count digitsn.ToString().Length or loop / 10
Armstrong testsum == n (exact integer compare)

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

Digit count k = 3. Peel digits from a temp copy and add each digit3.

tempDigitdigit3sum
15332727
155125152
111153
0stop153

Final check: 153 == 153 → Armstrong. (Order of digits in the sum does not matter.)

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

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

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

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.

Time and Space Complexity

ProgramTimeExtra space
Check one numberO(d · k) with IPow (d = k)O(1)
Often quotedO(log n) digit passesO(1)
Range 1…mO(m · log m) class workO(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

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.

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