How to Print Even Numbers from 1 to N in C (Using While & Do-While Loops)
In this tutorial, we will learn how to print even numbers from 1 to N
using while and do-while loops in C. Understanding how to control loop
output is a fundamental skill that frequently pops up in coding interviews and university exams.
Even numbers are simply those that can be divided by 2 without leaving a remainder—like 2, 4, 6, 8, and so on.
What is an even number in C?
An even number is any integer that is perfectly divisible by 2. In programming, we identify these by checking if the remainder is zero when divided by 2.
How to print even numbers from 1 to N in C?
The logic is straightforward: we run a loop from 1 to N and use the modulus operator to filter the numbers.
If the condition (number % 2 == 0) is true, we print it.
💻 C Program Code to Print Even Numbers
📌 Output
Enter the value of N: 7
Even Numbers from 1 to 7:
2 4 6
🧠 How This Program Works
- Step 1: Take the input value N from the user to set the range.
- Step 2: Initialize a counter (c) starting at 1.
- Step 3: Run the loop as long as the counter is less than or equal to N.
- Step 4: Check the even condition
(c % 2 == 0).
- Step 5: If the remainder is 0, print the number.
- Step 6: Increment the counter by 1 and repeat the process until the loop finishes.
⚡ Key Concept: The Modulus Operator
In C programming, we use the modulus (%) operator to find the remainder of a division.
It is the most efficient way to check if a number is even.
If a number divided by 2 gives a remainder of 0, that number is even.
Otherwise, it is odd. This simple check (c % 2 == 0) is the foundation of many
data filtering tasks you will encounter in real-world programming.
🚀 Practice Question
Try modifying this program to print:
- Odd and even numbers from 1 to N
- Sum of Odd numbers between range
Frequently Asked Questions (FAQs)
Q: How can I print even numbers from 1 to N in C?
A: Run a loop from 1 to N and check each number using (number % 2 == 0). If the condition is true, print the number.
Q: What is the easiest way to print even numbers in C?
A: The easiest way is to start from 2 and increase by 2 each time (2, 4, 6, 8...). This avoids checking every number.
Q: What condition is used to check even numbers in C?
A: We use (number % 2 == 0). If the remainder is 0, the number is even.
Q: Can I use both while and do-while loops for this program?
A: Yes, both loops work. The only difference is that a do-while loop runs at least once before checking the condition.
Q: Why is the modulus (%) operator important in C?
A: The modulus operator helps find the remainder. It is very useful for checking conditions like even/odd numbers.
Q: Is this question important for exams or interviews?
A: Yes. This is a very common beginner-level question and helps you understand loops and conditions clearly.
🔥 Print Even Numbers in C using Do-While Loop: A Step-by-Step Guide
Let’s learn how to print even numbers from 1 to N using a do-while loop in C.
Even numbers are those which are divisible by 2, like 2, 4, 6, 8…
Code