How to print all the Armstrong Numbers from 1 to 1000 using C#?


To display Armstrong numbers from 1 to 100, firstly use a while loop.

Example

while (val <= 1000) {
}

Now inside the while loop, set conditions for first, second and third digit.

Example

d1 = val - ((val / 10) * 10);
d2 = (val / 10) - ((val / 100) * 10);
d3 = (val / 100) - ((val / 1000) * 10);

Since, Armstrong number checks for the cube of all the digits.

Example

res = (d1 * d1 * d1) + (d2 * d2 * d2) + (d3 * d3 * d3);
if (res == val) {
   Console.WriteLine(temp);
}

If the cube of all the digits is equal to the number itself, then the number is an Armstrong number, for example, 153.

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

340 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements