even_loop.c
Code:
Another valid solution:
#include <stdio.h>
int main(void) {
// print even numbers between 27 and 63
for (int e = 28; e <= 62; e = e + 2) {
printf("%d\n", e);
}
}
#include <stdio.h>
int main(void) {
// print even numbers between 27 and 63
for (int n = 27; n <= 63; n++) {
if (n % 2 == 0) {
printf("%d\n", n);
}
}
}
The first solution knows that even numbers are separate by 2, and so does "skip-counting", increasing the variable e by 2 each time through the loop and always printing out the value.
The second solution runs through all the numbers from 27 to 63, increasing the variable n by 1 each time through the loop. Then in the body of the loop it tests whether n is even or not, and only prints the number if it is even.
Terminal window:
davidrhmiller@ide50:~/workspace/meeting_3_prework $ make even_loop
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow even_loop.c -lcs50 -lm -o even_loop
davidrhmiller@ide50:~/workspace/meeting_3_prework $ ./even_loop
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62