don't get it. I have a main with just some code to tell me hoy many numbers are multiple of 3 or 5. So, here's the code to see: https://paste.ofcode.org/77fqtRzi6DkPJqDHY6HWNr
What I don't get is how does it work with those scanf(): one outside of the while loop saves me from having to initalize the variable num to 0 so it doesn't break the while. And the scanf() inside of the while loop checks all the numbers from my input to see if there's a 0 to end the while. If I delete one of those scanf(), it doesn't work. How's that?
If you delete the inner scanf, there is no terminating condition for your loop. Also, that inner scanf is the only thing that stops your loop. If you remove that one, then suppose you enter 15 in the outer scanf, then the program will enter an infinite loop. Each time checking if 15 is a multiple of 3 and 5, then incrementing cont and doing the same thing again and never stopping.
Use “long long int” instead of “int”; and you know what to change from %d.. to..
you can simplify this as int num; int cont = 0; while (scanf("%d", &num) == 1 && num > 0) { if (/* ... */) { /* ... */ } }
Обсуждают сегодня