-2
Programming exercise in C: Statement: "Write a program that prints all pairs between 2 and 50. To know if the number is even, just see if the rest of the division by 2 is equal to 0."
what I did:
#include <stdio.h>
int main() {
for(int n = 2; n <= 50; n++){
int rest = n % 2;
if(rest == 0){
printf("%d", n);
}
}
}
What you have printed: 2468101214161820222426283032343638404244464850
Detail that you can do the
for
skipping 2 by 2:for(int n = 2; n <= 50; n += 2)
- then you don’t even have to test the rest of the division, because all the numbers will surely be even (unless, of course, the exercise requires you to do one on one and use the operator%
)– hkotsubo
Thank you very much! Your reply and helped in another exercise!!!
– Jeferson Tomaz