简体   繁体   中英

Back to scanf and dont run the loops

Ok why my code always back me to scanf? I don't know what i made wrong in this code but the code only backup me at scanf only reading numbers

int main()
{
    int m,n,i,tmp,min=9999999;
    scanf("%d%d",&m,&n); // scanf m and n right ?
    for(i=m;i<=n;i++) // for loop
    {
        tmp=i; // we give tmp a i value 
        while(tmp%10%2 == 0) 
        {
            tmp/=10;// in this while loop we check if the number is made only of even numbers
        }
        if(tmp!=0) // when the while loop break  we go down here right ? and if the number isn't made only of even digits we printf No
        printf("NO");
        else{ // else if its made of we check that that number is smaller then min number made of even digits
            if(i<min)
            min=i;
        }

    }
printf("%d",min); // on the end of the for loop we printf the min number made of even digits 
}

So why my compiler don't wont to run this program?

Your program enters infinite loop on the first number made of every even number. You cut digit and guide tmp till zero which falls to infinite loop: 0 % 10 % 2 is zero, and result of 0 / 2 is zero. You may fix it this way:

while (tmp > 0 && (tmp % 10) % 2 == 0) { /* ... */ }

I'm guessing you aren't having trouble compiling, but, instead, you run the code and nothing happens.

I think you're imagining that with "scanf("%d%d",&m,&n)", if you type in "1 30", that m will be 1 and n will be 30 and scanf will return 2. But actually scanf will return 1 because it stops parsing when it sees the space that isn't reflected in your format string, so m will be 1, n will be technically random, but probably 0 in your case, and your loop will "run" from 1 to 0, ie, zero times. Try "%d %d". And check the return code from scanf to make sure 2 numbers are entered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM