(C language 3) Three-digit daffodil number output

 answer:

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

analyze:

In fact, you only need to decompose a three-digit number and multiply it by the cube.

Because int is an integer; so ( i /100 ) is also an integer, and division in C language is directly rounded, not rounded;

First separate the hundreds out: ( i / 100 ) then cube;

Separation of tens: first (i % 100) take the remainder, after taking the remainder, you will get a two-digit number, then / 10, you can get the number on the tens unit of the two-digit number, and then cube it;

In the same way, separate the single digits again: the digits are directly a three-digit number ( i %10 ), then take the remainder to get the single digits, and then cube

Finally add it up, which is the above

i==(i/100)*(i/100)*(i/100)+((i%100)/10)*((i%100)/10)*((i%100)/10)+(i%10)*(i%10)*(i%10)

It can be realized with an accumulated for statement and if.

This question comes from the question number 1016 of C Language Network

Guess you like

Origin blog.csdn.net/a871923942/article/details/130652143