Check if a Number is an Armstrong Number in C#



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();
Updated on: 2020-06-22T07:21:29+05:30

275 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements