Write a C program to print all even numbers from 1 to n using for loop. C program to generate all even numbers between given range. How to display even numbers between 1 to n using for loop in C programming. Logic to print even number using if else and for loop in given range in C program.
Example
Input
Input upper range: 10
Output
Even numbers between 1 to 10: 2, 4, 6, 8, 10
Required knowledge
Basic C programming, If else, For loop
There can be various approaches to print even numbers in C. Here I am explaining the two common and beginner methods.
Logic to print even numbers using if condition
This is the first most chosen method to print even numbers. Before I formally get to the logic, below are two prerequisite for this logic.
Let us get to the logic to print even number using if condition.
- Read upper limit of the even number to be printed from user. Store it in some variable say N.
- Initialize a loop from 1, that runs till N, incrementing 1 in each iteration. The loop structure should look like for(i=1; i<=N; i++).
- Inside the loop body check the even condition. If the current number i.e. i is even. Which is if(i%2==0), then print the value of i.
Program to print even numbers using if condition
/** * C program to print all even numbers from 1 to n */ #include <stdio.h> int main() { int i, n; // Read upper limit of even number from user printf("Print all even numbers till: "); scanf("%d", &n); printf("Even numbers from 1 to %d are: \n", n); /* * Start loop counter from 1, increment it by 1, * will iterate till n */ for(i=1; i<=n; i++) { // Check even condition before printing if(i%2==0) { printf("%d\n", i); } } return 0; }
As I spoke earlier, let us see the other approach to print even numbers. But before we move on to the other method, you must question why other? What's the problem with above one? The above method is simple and easiest to implement. But it is not the optimal way for the given problem statement. We unintentionally iterating over all natural numbers between 1 to n. We intended only to print even numbers. Then how to do so? Let us now talk about that.
Logic to print even numbers without if statement
Below is the step by step descriptive logic to print even numbers from 1 to n.
- Read upper limit to print even number from user. Store it in some variable say n.
- This time run a loop from 2, that goes till n, incrementing 2. So the loop structure looks like for(i=2; i<=n; i+=2). Let us check the loop details.
Why started from 2? Why not, as we need to print even numbers from 1 to n. Hence the first even number is 2. So why not, start with 2 instead of 1.
Why incrementing the loop by 2 i+=2, not by 1 i++? Oh! come on a kid can answer. If i is even number, then to get next even we need to add 2, not 1. - Finally inside loop body print the value of i.
Program to display even numbers without using if statement
/** * C program to display all even numbers from 1 to n without if */ #include <stdio.h> int main() { int i, n; //Read 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, * for each repetition */ for(i=2; i<=n; i+=2) { printf("%d\n",i); } return 0; }
Note: Instead of i+=2, you can also use i = i + 2. Both convey the same meaning.
Print all even numbers till: 100 All even numbers from 1 to 100 are: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
Finally let us write the program to print even number in a given range.
Program to print even number in range
/** * C program to display all even numbers from 1 to n without if */ #include <stdio.h> int main() { int i, start, end; //Read upper and lower limit printf("Enter lower limit: "); scanf("%d", &start); printf("Enter upper limit: "); scanf("%d", &end); printf("All even numbers from %d to %d are: \n", start, end); // If start is not even make it even if(start%2!=0) { start++; } /* * Initialize loop from start and increment by 2, * for each repetition */ for(i=start; i<=end; i+=2) { printf("%d\n",i); } return 0; }
Before moving on to next exercise or program. Enhance your skill and learn some other approaches to solve this program.
Enter lower limit: 40 Enter upper limit: 50 All even numbers from 40 to 50 are: 40 42 44 46 48 50
Happy coding ;)
You may also like
- Loop programming exercises index.
- C program to print all natural numbers between 1 to n.
- C program to print all alphabets from a to z.
- C program to print all odd numbers between 1 to 100.
- C program to print sum of all even numbers between 1 to n.
- C program to print sum of all odd numbers between 1 to n.
- C program to enter any number and calculate its factorial.