# Write a Program to Check Armstrong Number in C#

Do you need to check armstrong number in C#? In this C# tutorial, I will explain to you how to write a program to check Armstrong number in C#.

To determine if a number is an Armstrong number in C#, calculate the sum of its digits each raised to the power of the number of digits. If this sum matches the original number, it’s an Armstrong number. Here’s a snippet:

``````int sum = 0, temp = number;
while (temp > 0) { sum += (int)Math.Pow(temp % 10, number.ToString().Length); temp /= 10; }
bool isArmstrong = number == sum;
``````

## Write a Program to Check Armstrong Number in C#

Let us see, how to write a program in C# to check whether a number is an Armstrong number or not.

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number because 1 power 3+5 power 3 + 3 power 3 = 15313+53+33 = 153. This concept can be extended to n-digit numbers too.

The logic is straightforward: we take a number, split it into its individual digits, raise each digit to the power of the number of digits in the number, sum these powers up, and compare the sum to the original number.

``````using System;

class ArmstrongNumber
{
static void Main(string[] args)
{
Console.Write("Enter a number: ");

int sum = 0, temp = number;
int digits = number.ToString().Length;

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

Code:

1. Input a Number: We start by taking an integer input from the user.
2. Calculate the Number of Digits: We need to know how many digits are there in the number to raise each digit to the power of this count.
3. Split and Sum the Powers: We then split the number into its digits, raise each digit to the power of the number of digits, and sum these values.
4. Check and Output: Finally, we compare this sum with the original number to determine if it’s an Armstrong number.

You can see here in the below screenshot, after I ran the code using Visual Studio.

## Conclusion

This tutorial guides on writing a C# program to check for Armstrong numbers. We’ve discussed what Armstrong numbers are, the logic behind checking for them, and provided a full code example. I hope this tutorial will help you to Write a Program to Check Armstrong Number in C#.

You may also like: