In this article, we will learn about Armstrong numbers and create a C# program to check whether a given number is an Armstrong number or not.

Table of Contents

### What is an Armstrong Number?

An Armstrong number is a number that is the sum of its own digits, and each is raised to the power of the number of digits. For example, the few Armstrong numbers are 0, 1, 153, 371, 407, 1634, 8208, and 9474.

Let’s take the number 153:

1^{3}+5^{3}+3^{3}

= (1*1*1) + (5*5*5) + (3*3*3)

= 1+125+27

= 153.

In this case, 153 is an Armstrong number because the sum of the cubes of its digits equals the number itself.

### Getting Started:

Let’s jump into the code and start understanding and implementing a C# Program to Check Armstrong Number.

#### Code Example 1: C# Program to Check Armstrong Number

```
using System;
public class ArmstrongChecker
{
public static void Main(string[] args)
{
int number, sum = 0, originalNumber;
Console.Write("Enter the Number: ");
number = int.Parse(Console.ReadLine());
originalNumber = number;
while (number > 0)
{
int digit = number % 10;
sum += (int)Math.Pow(digit, 3);
number /= 10;
}
if (originalNumber == sum)
{
Console.WriteLine($"{originalNumber} is an Armstrong number.");
}
else
{
Console.WriteLine($"{originalNumber} is not an Armstrong number.");
}
}
}
```

**Output:**

```
Enter the Number: 153
153 is an Armstrong number.
```

```
Enter the Number: 127
127 is not an Armstrong number.
```

## C# Code Example 2: Using Function

```
using System;
class ArmstrongNumberChecker
{
static void Main()
{
Console.Write("Enter a number: ");
int number = int.Parse(Console.ReadLine());
int originalNumber = number;
int result = 0;
int power = CountDigits(number);
while (number > 0)
{
int digit = number % 10;
result += (int)Math.Pow(digit, power);
number /= 10;
}
if (result == originalNumber)
{
Console.WriteLine($"{originalNumber} is an Armstrong number.");
}
else
{
Console.WriteLine($"{originalNumber} is not an Armstrong number.");
}
}
static int CountDigits(int num)
{
int count = 0;
while (num > 0)
{
num /= 10;
count++;
}
return count;
}
}
```

**Output:**

```
Enter a number: 371
371 is an Armstrong number.
```

**Code Explanation:**

- The above C# program asks the user to input a number.
- It then uses a helper function named
`CountDigits`

to calculate the power needed for Armstrong number checking by counting the digits in the given number. **Summation of Digits**: It then iterates through each digit of the number, raises it to the calculated power, and adds the results together.**Comparison**: Finally, it compares the computed sum with the original number to determine whether it’s an Armstrong number or not.

**Here are a few Armstrong numbers:**

- 1 (1^1 = 1)
- 153 (1^3 + 5^3 + 3^3 = 153)
- 407 (4^3 + 0^3 + 7^3 = 407)
- 1634 (1^4 + 6^4 + 3^4 + 4^4 = 1634)
- 8208 (8^4 + 2^4 + 0^4 + 8^4 = 8208)
- 9474 (9^4 + 4^4 + 7^4 + 4^4 = 9474)

These numbers satisfy the Armstrong number criteria, where the sum of each digit raised to the power of the number of digits equals the original number.

**Recommended Articles:**

- C# Program to Convert Decimal to Binary with Examples
- C# Program to Convert Binary to Decimal with Examples
- C# Program to Convert Hexadecimal to Decimal
- How to remove duplicate characters from a String in C#
- C# program to count the occurrences of each character in a String
- C# Program to Print Multiplication Table of a Given Number
- Leap Year Program in C, C++, C#, JAVA, PYTHON, and PHP
- Program to print prime numbers from 1 to N
- C# Program to Check if a Given Number is Even or Odd
- Program to copy all elements of an array into another array
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
**C# Programs asked in Interviews**

- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024
- How to Reverse an Array in C# with Examples - February 12, 2024