Online Snippets Library

Printing even values in c using loop

To Print even values we need to use conditional statement (if-else) and % (mode) operator. A Varible is Even if It completely divisible by 2.Otherwise it is odd.

C Program
#include <stdio.h>
int main() {
   int i;

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

Output

2 4 6 8 10 12 14