# C# Program - Check Armstrong Number

A positive natural number is known as Armstrong number of order $n$ if it can be expressed as the sum of each digits of the number raised to the power of $n$.

$A_1A_2A_3...A_n = \sum_{i=0}^n A_i^n = A_1^n+A_2^n+A_3^n+...A_n^n$

### Examples:

$153 = 1^3+5^3+3^3 = 1+125+27 = 153$

$371 = 3^3+7^3+1^3 = 27+343+1 = 371$

$1634 = 1^4+6^4+3^4+4^4 = 1+1296+81+256 = 1634$

### Example: Check Armstrong Number

In the below example, the MyNum is checked for Armstrong number using method called ArmStrongNum(). The method requires two parameters, first the number and second the number of digits in it. It calculates power of the digit using the Pow() method. Please see the example below for syntax:

using System;

namespace MyApplication {
class MyProgram {
//Calculate power of a digit
static int Pow(int MyNum, int n) {
int x = 1;
while(n > 0)
{
x = x*MyNum;
n--;
}
return x;
}

static void ArmStrongNum(int MyNum, int Order) {
int y = MyNum;
int sum = 0;
while (y > 0){
int x = y % 10;
sum = sum + Pow(x, Order);
y = y/10;
}
if (MyNum == sum){
Console.WriteLine(MyNum + " is a Armstrong Number.");
} else {
Console.WriteLine(MyNum + " is not a Armstrong Number.");
}
}

static void Main(String[] args) {
ArmStrongNum(371, 3);
ArmStrongNum(1634, 4);
ArmStrongNum(1000, 4);
}
}
}


Output

371 is a Armstrong Number.
1634 is a Armstrong Number.
1000 is not a Armstrong Number.


### Example: Armstrong Number of order $n$

Here, the ArmStrongNum method requires only one parameter, the number itself. The number of digits in the passed number is estimated inside the method. Please see the example below for syntax:

using System;

namespace MyApplication {
class MyProgram {
//Calculate power of a digit
static int Pow(int MyNum, int n) {
int x = 1;
while(n > 0)
{
x = x*MyNum;
n--;
}
return x;
}

static void ArmStrongNum(int MyNum) {
int y = MyNum;
int sum = 0;
int Order = 0;
//Find number of digit in the Number
while(y > 0){
Order++;
y = y / 10;
}

y = MyNum;
while (y > 0){
int x = y % 10;
sum = sum + Pow(x, Order);
y = y/10;
}
if (MyNum == sum){
Console.WriteLine(MyNum + " is a Armstrong Number.");
} else {
Console.WriteLine(MyNum + " is not a Armstrong Number.");
}
}

static void Main(String[] args) {
ArmStrongNum(153);
ArmStrongNum(9474);
ArmStrongNum(5000);
}
}
}


Output

153 is a Armstrong Number.
9474 is a Armstrong Number.
5000 is not a Armstrong Number.