C# Program to Check Armstrong Number with Output
if (sum == number) Console.WriteLine("Armstrong number"); else Console.WriteLine("Not Armstrong number");.Examples
How to Think About It
Algorithm
Code
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"); } }
Dry Run
Let's trace the number 153 through the code to check if it is an Armstrong number.
Count digits
temp = 153; digits = 0; Loop: digits=1 (temp=15), digits=2 (temp=1), digits=3 (temp=0)
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
Compare sum and number
sum=153 equals number=153, so it is an Armstrong number.
| Iteration | Digit | Sum |
|---|---|---|
| 1 | 3 | 27 |
| 2 | 5 | 152 |
| 3 | 1 | 153 |
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 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"); } }
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"); } }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Numeric calculation | O(d) | O(1) | Efficiency and low memory |
| String conversion | O(d) | O(d) | Simplicity and readability |
| Recursive method | O(d) | O(d) | Elegant code but higher stack use |
Math.Pow to raise digits to the power of the digit count when checking Armstrong numbers.