Home  >  Article  >  Backend Development  >  How to print all Armstrong numbers from 1 to 1000 using C#?

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

王林
王林forward
2023-09-02 17:01:04629browse

如何使用 C# 打印从 1 到 1000 的所有阿姆斯特朗数字?

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);
}

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete