Published: 11 Dec 2024
Reading Time: 6 min read
Sorting numbers into even and odd is often something we all do on a daily basis, like buying items or making decisions. In simple terms, even numbers split perfectly into pairs like 2, 4, and 6 and are divisible by 2. Odd numbers always leave one behind, like 1, 3, and 5 and are not divisible by 2. The even-odd program in C takes this mathematical logic and turns it into a tool that's needed for many operations. In this blog, we will look at different ways to write the even-odd program in C.
The logic for the even-odd program in C involves checking if a number is divided evenly by 2. Here's how it works, step by step:
START
// Prompt the user to enter a number
PRINT "Enter a number"
READ number
// Check if the number is divisible by 2
IF number % 2 == 0 THEN
PRINT "The number is even"
ELSE
PRINT "The number is odd"
END IF
END
In this code we use a for loop that starts from 0 and increments by 2 checking even numbers. It asks the user to enter a number. If the entered number matches one of these even numbers, it prints that the number is even. If the loop completes without finding the number, it prints that the number is odd.
#include <stdio.h>
int main() {
int num;
// Ask the user to input a number
printf("Enter a number: ");
scanf("%d", &num);
// Use the modulus operator to check if the number is even or odd
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
Enter a number: 2
2 is even.
=== Code Execution Successful ===
We can use an array to store multiple numbers and check if each one is even or odd. You have to first input the number of elements. Then, it is followed by the numbers themselves. The program then iterates through the array and checks each number using the modulo operator to determine if it's divisible by 2. If the number is divisible by 2, it's printed as even, otherwise, it's printed as odd.
#include <stdio.h>
int main() {
int n, size;
// Ask the user for the number of elements
printf("Enter the number of elements: ");
scanf("%d", &size);
int numbers[size];
// Input the elements into the array
printf("Enter %d numbers:\n", size);
for (n = 0; n < size; n++) {
scanf("%d", &numbers[n]);
}
// Check if each number is even or odd
for (n = 0; n < size; n++) {
if (numbers[n] % 2 == 0) {
printf("%d is even.\n", numbers[n]);
} else {
printf("%d is odd.\n", numbers[n]);
}
}
return 0;
}
Enter the number of elements: 5
Enter 5 numbers:
4
7
10
15
20
4 is even.
7 is odd.
10 is even.
15 is odd.
20 is even.
In this code, the program uses the bitwise AND operator (&) to check if a number is even or odd. By performing a bitwise AND with 1 (num & 1), it examines the least significant bit of the number. If the result is 1, the number is odd; if it's 0, the number is even.
// C Program to Check Even or Odd Using Bitwise
// AND Operator
#include <stdio.h>
void checkEvenOdd(int N) {
// Check if the number is even or odd using bitwise
// AND operator
if (N & 1) {
printf("Odd\n");
}
else {
printf("Even\n");
}
}
int main() {
int N = 7;
checkEvenOdd(N);
return 0;
}
Odd
#include <stdio.h>
// Function to check if a number is even or odd
void checkEvenOdd(int num) {
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
}
int main() {
int num;
// Ask the user to input a number
printf("Enter a number: ");
scanf("%d", &num);
// Call the function to check if the number is even or odd
checkEvenOdd(num);
return 0;
}
Enter a number: 3
3 is odd.
=== Code Execution Successful ===
// C Program to Check Even or Odd Using Modulo Operator
#include <stdio.h>
void checkOddEven(int N) {
// Find the remainder
int r = N % 2;
// Condition for even
if (r == 0) {
printf("Even");
}
// Condition for odd number
else {
printf("Odd");
}
}
int main() {
int N = 101;
checkOddEven(N);
return 0;
}
Odd
In this program, the code checks are done using the ternary operator. It prompts the user to enter an integer and then checks if the number is divisible by 2. If true, it prints "even"; otherwise, it prints "odd." The ternary operator simplifies the logic into a single line. For example, entering 4 will output 4 is even, and 5 will output 5 is odd.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num);
return 0;
}
Learning the even or odd program is an important step in building foundational programming skills. It helps solidify understanding of basic concepts like loops, conditionals, and operators. These concepts are essential for more complex coding tasks in the future. Mastering such simple programs sets you up with the skills for developing proficiency in problem-solving and logic. To learn more, enroll into the CCBP 4.0 Academy and start you journey to become job-ready.
Learning these helps develop logical thinking and understanding of conditionals, operators, and loops. These are important for building more complex algorithms.
Zero will be considered as an even number.
Yes, all the methods mentioned above can also handle negative numbers.
The time complexity of all methods is more or less the same. However bitwise methods might be slightly faster.
You can use a for loop or a while loop to iterate through a series of numbers. Then, apply the even-odd check inside the loop.
Complete Guide on String Functions in C - Learn essential string functions in C with syntax, examples, memory rules, and safe practices. Master strlen, strcpy, strcmp, strcat, strstr, and more with clarity. (29 Dec 2025, 5 min read)
Mastering Insertion Sort in C: Code, Logic, and Applications - Understand insertion sort in C with easy-to-follow logic, code examples, and practical tips. Learn how this sorting technique works in real programs. (29 Dec 2025, 6 min read)
Quick Sort Algorithm Explained: Steps, Code Examples and Use Cases - Learn the Quick Sort Algorithm with clear steps, partition logic, Python & C++ code examples, and time complexity explained for students and developers. (28 Dec 2025, 6 min read)
Switch Statement in C: Syntax, Flowchart & Sample Programs - Learn how to use the switch statement in C programming with simple syntax, real-life examples, and use cases. Ideal for beginners and students. (27 Dec 2025, 6 min read)
The Ultimate Guide to Binary Search Algorithm in C - Learn the Binary Search Algorithm with steps, examples, and efficiency insights. Understand how it works in C for fast and accurate data searching. (27 Dec 2025, 8 min read)
Merge Sort in C: A Step-by-Step Guide with Code Examples - Learn how Merge Sort works in C with easy-to-follow examples, step-by-step logic, and code implementation. Ideal for beginners and coding interviews. (26 Dec 2025, 8 min read)