C Programming Code Examples C > For Loops and While Loops Code Examples Program to print even numbers without using if statement Program to print even numbers without using if statement Logic to print even numbers without if statement The above approach to list even numbers is not optimal. It unnecessarily iterates for odd numbers which is a performance issue. To overcome this start the loop with first even number. We know if n is an even number then n + 2 is the next even number. Hence, to get next even number just add 2 to the current even number. Input upper limit to print even number from user. Store it in some variable say n. Run a loop from first even number i.e. 2 (in this case), that goes till n and increment the loop counter by 2 in each iteration. So the loop structure looks like for(j=2; j<=n; j+=2). Finally inside loop body print the value of j. /* C program to display all even numbers from 1 to n without if */ #include <stdio.h> int main() { int j, n; /* Input upper limit of even number from user */ printf("Print all even numbers till: "); scanf("%d", &n); printf("All even numbers from 1 to %d are: \n", n); /* Start loop from 2 and increment by 2, in each repetition */ for(j=2; j<=n; j+=2) { printf("%d\n",j); } return 0; }