Write a C# program to check if the entered number is Armstrong number?

CsharpProgrammingServer Side Programming

A number is an Armstrong number if the sum of the cube of each digit of the number is equal to the number itself.

Here, we will find out the remainder and will sum it to the cube of remainder.

rem = i % 10;
sum = sum + rem*rem*rem;

Then if the above sum that comes out after loop iteration is equal to the sum, then it will be an Armstrong number.

if (sum == num) {
   Console.Write("Armstrong Number!");
}

The following is an example −

Example

int num, rem, sum = 0;
// checking for armstrong number
num = 153;

for (int i = num; i > 0; i = i / 10) {
   rem = i % 10;
   sum = sum + rem*rem*rem;
}

if (sum == num) {
   Console.Write("Armstrong Number!");
}
else
Console.Write("Not an Armstrong Number!");
Console.ReadLine();
raja
Published on 16-Aug-2018 12:28:29
Advertisements