Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What's wrong with the yellow label? Why is the result strange? What is wrong with the code?
What's wrong with the yellow label? Why is the result strange? What is wrong with the code?
The yellow label should mean warning, warning.

You want to find prime numbers and prime numbers between 2 and 100, right? Say some questions.

1. On the yellow line, you wrote for(j = 2;; j & lt= m; J++), but the type of m is double. You know, in a computer, there will be an error in the double type, that is, if m is 3, it may actually be 2.99999 or 3.00000 1 in a computer, then using it to compare the size with j (an integer) will cause problems in the control loop. It is suggested to change the type of m to int, and then change m=sqrt(i) to m=round(sqrt(i)), where round is a rounding function.

2. In the two yellow lines below, if (I% j = = 0) t =1; Break; There, they should be nested with braces, that is, if (I% j = = 0) {t =1; Break; }, otherwise it will lead to odd output, as to why you can think about it.

3. Didn't you output the value of k at last? Don't output or forget it?

Everything else should be fine. Ask me if I'm right.

Code:

# include & ltcstdio & gt

# include & ltmath.h & gt

int? main(){

int? I,j,k=0,t=0,m;

for(I = 2; I<= 100; i++){

t = 0;

m = round(sqrt(I));

for(j = 2; j & lt= m; j++){

if(i%j==0){

t = 1;

Break;

}

}

if(t==0){

printf("%5d ",I);

k++;

If (k%5==0)

printf(" \ n ");

}

}

Return? 0;

}