C language practice question 13

foreword

Why did I start playing again, and quickly sent all the drafts I had hoarded.

Thirteenth question

Topic:
Print out all the "Daffodil Numbers". The so-called "Daffodil Number" refers to a three-digit number whose cube sum is equal to the number itself. For example: 153 is a "daffodil number", because 153=1 cube +5 cube +3 cube.

My idea:
Use the for loop to control 100-999 numbers, and decompose each number into ones, tens, and hundreds. Variables are used to define them respectively.
1. Input: 100-999 numbers
Output: Number of daffodils

my process

#include<stdio.h>
 
int main()
{
    
    
    int i,x,y,z;
    for(i=100;i<1000;i++)
    {
    
    
        x=i%10;
        y=i/10%10;
        z=i/100%10;
        
        if(i==(x*x*x+y*y*y+z*z*z))
        printf("%d\n",i);
        
    }
    return 0;
}

operation result
insert image description here

Summarize

Guess you like

Origin blog.csdn.net/weixin_52248428/article/details/116432327