Easy Tutorial
❮ C Exercise Example47 C Scope Rules ❯

C Language Examples - Looping Through Odd/Even Numbers in a Range

C Language Examples

Looping through odd/even numbers in a specified range can be determined by the remainder when divided by 2.

The following example loops through and prints even numbers within a specified range.

Example

#include <stdio.h>

int main() {
   int i;

   for(i = 1; i <= 10; i++) {
      if(i%2 == 0)
         printf(" %2d\n", i);
   }
   return 0;
}

Output:

2
4
6
8
10

The following example loops through and prints odd numbers within a specified range.

Example

#include <stdio.h>

int main() {
   int i;

   for(i = 1; i <= 10; i++) {
      if(i%2 != 0)
         printf("%d\n", i);
   }
   return 0;
}

Output:

1
3
5
7
9

C Language Examples

❮ C Exercise Example47 C Scope Rules ❯