Home > Java > javaTutorial > How to output the number of all daffodils in the console in java

How to output the number of all daffodils in the console in java

WBOY
Release: 2023-05-18 13:37:06
forward
2112 people have browsed it

Daffodil

Title: Output the number of all daffodils on the console

What is the number of daffodils?

  • Daffodil is a three-digit number;

  • The sum of the cubes of the ones, tens, and hundreds of the narcissus number is equal to the original number.

Analysis:

  • Use a loop to traverse all three digits (starting at 100 and ending at 999);

  • Get the value of each digit in the three-digit number before calculation;

  • Take out each value in the three-digit number, calculate the cubic sum and combine it with the original value Compare;

  • Output the number of daffodils.

Practice:

The code is as follows (example):

public class Demo1 {
    public static void main(String[] args) {
    //不爱生姜不吃醋
        //使用循环遍历所有的三位数(100开始到999结束);

        for(int i=100;i<1000;i++){
            // 计算之前获取三位数中的每个位上的值;
            int ge=i%10;
            int shi=i/10%10;
            int bai=i/100%10;

            // 将三位数中的每个数值取出来,计算立方和并与原数值比较;
            if(ge*ge*ge+shi*shi*shi+bai*bai*bai == i){
                // 输出水仙花数。
                System.out.print(i+" ");
            }
        }
    }
}
Copy after login

The above is the detailed content of How to output the number of all daffodils in the console in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.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