Home > Article > Backend Development > How to print all Armstrong numbers from 1 to 1000 using C#?
To display Armstrong numbers from 1 to 100, firstly use a while loop.
while (val <= 1000) { }
Now inside the while loop, set conditions for first, second and third digit.
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.
res = (d1 * d1 * d1) + (d2 * d2 * d2) + (d3 * d3 * d3); if (res == val) { Console.WriteLine(temp); }
A number is an Armstrong number if the sum of the cubes of each digit of the number is equal to the number itself, for example, 153.
The above is the detailed content of How to print all Armstrong numbers from 1 to 1000 using C#?. For more information, please follow other related articles on the PHP Chinese website!