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

王林
Release: 2023-09-02 17:01:04
forward
565 people have browsed it

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

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

Example

while (val <= 1000) {
}
Copy after login

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);
Copy after login

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);
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!