C Language Example - Loop Over Odd/Even in a Range
Looping to output odd/even numbers within a range can be determined by the remainder when divided by 2.
The following example outputs even numbers within a specified range using a loop.
Example
#include <stdio.h>
int main() {
int i;
for(i = 1; i <= 10; i++) {
if(i%2 == 0)
printf(" %2d\n", i);
}
return 0;
}
Run result:
2 4 6 8 10
The following example outputs odd numbers within a specified range using a loop.
Example
#include <stdio.h>
int main() {
int i;
for(i = 1; i <= 10; i++) {
if(i%2 != 0)
printf("%d\n", i);
}
return 0;
}
Run result:
1 3 5 7 9other extensions
C Language Examples