C Examples For Even Odd
## C Program to Print Even and Odd Numbers in a Given Range
In C programming, determining whether a number is even or odd is one of the most fundamental exercises. An integer is **even** if it is exactly divisible by 2, and **odd** if it is not.
This tutorial demonstrates how to use loops and conditional statements to find and print all even or odd numbers within a specified range.
---
## Mathematical Logic
To determine if a number $n$ is even or odd, we use the **modulo operator (`%`)**, which returns the remainder of a division operation:
* **Even Numbers:** `n % 2 == 0` (The remainder is 0 when divided by 2).
* **Odd Numbers:** `n % 2 != 0` (The remainder is 1 or -1 when divided by 2).
By combining this logic with a `for` loop, we can easily iterate through any range of numbers and filter them accordingly.
---
## Code Examples
### 1. Printing Even Numbers in a Range (1 to 10)
The following program iterates through numbers from 1 to 10 and prints only the even numbers.
```c
#include
int main() {
int i;
printf("Even numbers between 1 and 10:\n");
// Loop through the range 1 to 10
for(i = 1; i <= 10; i++) {
// Check if the number is divisible by 2
if(i % 2 == 0) {
printf(" %2d\n", i);
}
}
return 0;
}
```
#### Output:
```text
2
4
6
8
10
```
---
### 2. Printing Odd Numbers in a Range (1 to 10)
The following program iterates through numbers from 1 to 10 and prints only the odd numbers.
```c
#include
int main() {
int i;
printf("Odd numbers between 1 and 10:\n");
// Loop through the range 1 to 10
for(i = 1; i <= 10; i++) {
// Check if the number is not divisible by 2
if(i % 2 != 0) {
printf("%d\n", i);
}
}
return 0;
}
```
#### Output:
```text
1
3
5
7
9
```
---
## Performance Optimization (Alternative Approach)
While using the modulo operator (`%`) is highly intuitive, you can optimize the performance of your program by avoiding conditional checks inside the loop altogether.
### Optimized Even Number Loop
Instead of checking every number, you can initialize the loop variable to the first even number in the range and increment it by `2` in each iteration:
```c
#include
int main() {
int i;
printf("Optimized Even Numbers (1 to 10):\n");
// Start at 2 and increment by 2
for(i = 2; i <= 10; i += 2) {
printf("%d\n", i);
}
return 0;
}
```
### Optimized Odd Number Loop
Similarly, you can start at the first odd number (`1`) and increment by `2`:
```c
#include
int main() {
int i;
printf("Optimized Odd Numbers (1 to 10):\n");
// Start at 1 and increment by 2
for(i = 1; i <= 10; i += 2) {
printf("%d\n", i);
}
return 0;
}
```
### Bitwise Optimization
For advanced use cases, you can also use the **bitwise AND (`&`)** operator. This is computationally faster than the modulo operator because it operates directly on the binary representation of the number:
* `(i & 1) == 0` evaluates to `true` if the number is **even**.
* `(i & 1) != 0` evaluates to `true` if the number is **odd**.
```c
if ((i & 1) == 0) {
// Code for even numbers
}
```
---
## Summary of Methods
| Method | Pros | Cons |
| :--- | :--- | :--- |
| **Modulo Operator (`%`)** | Highly readable, easy to understand for beginners. | Slightly slower due to division operations. |
| **Step Increment (`i += 2`)** | Best performance, eliminates conditional checks. | Requires knowing the starting boundary parity (even/odd). |
| **Bitwise AND (`&`)** | Extremely fast execution at the hardware level. | Slightly less readable for beginners. |
YouTip