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.
Approach for 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:
- Prompt the user to enter a number.
- Capture the input and store it in an integer variable, like a number.
- Use an if statement to evaluate whether the number's remainder, when divided by 2, is zero (number % 2 == 0).
- If the remainder is 0, the program concludes the number is even and displays a message.
- If not, the number is odd, and the program outputs a corresponding message.
- Finally, the program ends.
The pseudocode for even and odd program:
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
Implementing Even Odd Program in C Using FOR Loop
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;
}
Output:
Enter a number: 2
2 is even.
=== Code Execution Successful ===
Even Odd Program in C Using Array
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;
}
Output:
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.
Even Odd Program in C Using Bitwise Operator
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;
}
Output :
Odd
Even Odd Program in C Using Function
- The program checks if a given number is even or odd.
- The checkEvenOdd function takes the input number (num).
- It uses the modulus operator (%) to check if the number is divisible by 2:
- Even check: If num % 2 == 0, the number is even.
- Odd check: If num % 2 != 0, the number is odd.
- Based on the result, the function prints whether the number is even or 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;
}
Output:
Enter a number: 3
3 is odd.
=== Code Execution Successful ===
Even-odd program in C using the Modulus operator
- The program checks if a number is even or odd using the modulo operator (%).
- The checkOddEven function takes an integer N as input. It then calculates the remainder when N is divided by 2 (N % 2). If the remainder is 0, it prints "Even". If the remainder is not 0, it prints "Odd".
- In the main function, the number 101 is passed to checkOddEven.
- The program prints "Odd" because 101 is not divisible by 2.
// 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;
}
Output:
Odd
Even-Odd Program in C Using Ternary Operator
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;
}
Conclusion
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.
Boost Your Placement Chances by Learning Industry-Relevant Skills While in College!
Explore ProgramFrequently Asked Questions
1. What is the purpose of checking if a number is even or odd in programming?
Learning these helps develop logical thinking and understanding of conditionals, operators, and loops. These are important for building more complex algorithms.
2. What happens if the input is zero for a even or odd program in C?
Zero will be considered as an even number.
3. Can even and odd program in C handle negative numbers?
Yes, all the methods mentioned above can also handle negative numbers.
4. Which of the even and odd methods is the fastest?
The time complexity of all methods is more or less the same. However bitwise methods might be slightly faster.
5. How do I use a loop to check multiple numbers for even or odd?
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.