C language case to print the number of colchicum flowers -05

Topic: Print out the number of all daffodils.

Step 1: Define Program Goals

    编写一个C程序,输出所有的水仙花数。

Step 2: Program Design

Principle: The so-called "narcissus number" refers to a three-digit number, and the cube sum of each digit is equal to the number itself. For example: 153 is a "narcissus number", because 153 = 1 cube + 5 cube + 3 Cubed.
Programming: Because the number of daffodils is a three-digit number, we can use the for loop to traverse, and then use if to judge each time through the traversal. If it is judged to be a daffodil number, we can use the printf function to output

code writing

#include<stdio.h>
#include<math.h>
int main(){
    
    
    int a,b,c,tmp;
    for(int i=100;i<=999;i++){
    
      //遍历所有的三位数
        //把三位数中的每个位置上的数字提取出来
        a=i/100;
        b=i/10%10;
        c=i%10;
        tmp=pow(a,3)+pow(b,3)+pow(c,3); //每个数分别进行立方后再求和
        if(i==tmp){
    
       //根据原理进行判定符合条件的数值
            printf("%d是水仙花数!\n",i);
        }
        
    }
}

Summarize

When designing a program, we may just think about implementing this program without thinking too much about it. This is a common phenomenon in many software projects, and it should be completed first and then perfected. My personal suggestion is that when programming, more consideration should be given to the expansion of the entire project and the simplicity of the project. Well, see you in the next chapter, come on!

Guess you like

Origin blog.csdn.net/weixin_37171673/article/details/132154678