0
0
CsharpProgramBeginner · 2 min read

C# Program to Check Armstrong Number with Output

To check an Armstrong number in C#, use a program that extracts each digit, raises it to the power of the number of digits, sums these values, and compares the sum to the original number, like if (sum == number) Console.WriteLine("Armstrong number"); else Console.WriteLine("Not Armstrong number");.
📋

Examples

Input153
Output153 is an Armstrong number
Input9474
Output9474 is an Armstrong number
Input123
Output123 is not an Armstrong number
🧠

How to Think About It

To check if a number is an Armstrong number, first find how many digits it has. Then, for each digit, raise it to the power of the total digits and add all these results. If the total sum equals the original number, it is an Armstrong number; otherwise, it is not.
📐

Algorithm

1
Get the input number.
2
Count the number of digits in the number.
3
For each digit in the number, raise it to the power of the digit count and add to a sum.
4
Compare the sum with the original number.
5
If they are equal, print that the number is an Armstrong number; otherwise, print it is not.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        int number = 153, sum = 0, temp, digits = 0;
        temp = number;
        while (temp > 0) {
            digits++;
            temp /= 10;
        }
        temp = number;
        while (temp > 0) {
            int digit = temp % 10;
            sum += (int)Math.Pow(digit, digits);
            temp /= 10;
        }
        if (sum == number)
            Console.WriteLine($"{number} is an Armstrong number");
        else
            Console.WriteLine($"{number} is not an Armstrong number");
    }
}
Output
153 is an Armstrong number
🔍

Dry Run

Let's trace the number 153 through the code to check if it is an Armstrong number.

1

Count digits

temp = 153; digits = 0; Loop: digits=1 (temp=15), digits=2 (temp=1), digits=3 (temp=0)

2

Calculate sum of powers

temp=153; sum=0; Extract digits: 3, 5, 1; sum=3^3=27, sum=27+5^3=27+125=152, sum=152+1^3=153

3

Compare sum and number

sum=153 equals number=153, so it is an Armstrong number.

IterationDigitSum
1327
25152
31153
💡

Why This Works

Step 1: Counting digits

We count digits by dividing the number by 10 repeatedly until it becomes 0, increasing the digit count each time.

Step 2: Summing powers

Each digit is raised to the power of the total digits using Math.Pow and added to a sum.

Step 3: Checking equality

If the sum equals the original number, it confirms the number is an Armstrong number.

🔄

Alternative Approaches

Using string conversion
csharp
using System;
class Program {
    static void Main() {
        int number = 9474, sum = 0;
        string strNum = number.ToString();
        int digits = strNum.Length;
        foreach (char c in strNum) {
            int digit = c - '0';
            sum += (int)Math.Pow(digit, digits);
        }
        Console.WriteLine(sum == number ? $"{number} is an Armstrong number" : $"{number} is not an Armstrong number");
    }
}
This approach uses string conversion to simplify digit extraction but may be less efficient for very large numbers.
Using recursion
csharp
using System;
class Program {
    static int PowerSum(int num, int digits) {
        if (num == 0) return 0;
        int digit = num % 10;
        return (int)Math.Pow(digit, digits) + PowerSum(num / 10, digits);
    }
    static void Main() {
        int number = 153;
        int digits = number.ToString().Length;
        int sum = PowerSum(number, digits);
        Console.WriteLine(sum == number ? $"{number} is an Armstrong number" : $"{number} is not an Armstrong number");
    }
}
This recursive method is elegant but may use more stack space for large numbers.

Complexity: O(d) time, O(1) space

Time Complexity

The program loops through each digit twice: once to count digits and once to calculate the sum, so time complexity is O(d) where d is the number of digits.

Space Complexity

Only a few integer variables are used, so space complexity is O(1), constant space.

Which Approach is Fastest?

The numeric approach without string conversion is generally faster and uses less memory than string-based or recursive methods.

ApproachTimeSpaceBest For
Numeric calculationO(d)O(1)Efficiency and low memory
String conversionO(d)O(d)Simplicity and readability
Recursive methodO(d)O(d)Elegant code but higher stack use
💡
Use Math.Pow to raise digits to the power of the digit count when checking Armstrong numbers.
⚠️
Beginners often forget to count the number of digits first, which is essential for the power calculation.