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

CsharpProgrammingServer Side Programming

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.

raja
Published on 20-Aug-2018 09:32:09
Advertisements