Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Checking whether a number is odd or even is one of the most basic yet essential programs in C. This concept is widely used in problem-solving and forms the foundation of logical thinking. In this blog, we’ll explore how to write an efficient even odd program in C.
What is C Language?
C programming language is one of the oldest languages in the programming community. C is a mid-level language means you can do system programming or application programming in C, which fills the gap between low-level and high-level languages. C is considered a good to learn programming language if you are a beginner because it will help clear the basic programming concepts necessary for a programmer.
Features of C
It is a robust programming language with various in-built functions, data types, and operators.
It is a platform-independent means code written in C can be executed on any system.
It is considered a fast programming language.
It is a procedural language.
Now, we know everything about the C programming language and its features. Next, let's see the problem statement in detail, which checks if a number is an odd or even number in C language. It is important to know the problem statement before implementing the odd or even program in c.
We must determine whether the given input number is odd or even in C.
To explain it, we need to check whether the given number is divisible by 2. If it is divisible by 2, then it is even; otherwise, it is odd.
For example, 2,4,10,8… are even numbers because they are divisible by 2 and 3,5,9… are odd numbers because they are not divisible by 2.
Example
Lets us understand the problem with an example, so we do not have remaining doubts and can easily understand the odd or even program in c and implement it on the Online C compiler.
Sample Input
4, 9
Sample Output
4 is an even number
9 is an odd number
Explanation
4%2 = 0 and 9%2 ≠ 0
Approach
We will check if the number is odd or even in c by taking the modulus ( % ) of the number with 2.
Modulus gives the remainder 0 if the numerator is divisible by the denominator. In this case, the numerator is user input, and the denominator is 2. If the remainder of the number modulus by 2 is 0, then the number is even; otherwise, it is odd. The complete statement will look like the following.
number%2==0
Before we discuss the implementation, you need to know about the conditional statements in C because we have used conditional statements in the program.
Conditional statements, also known as an if-else statements, mean if a particular condition is true, then the program will run a statement, and if it is not true (else), then the program will execute the line of code written inside else.
This DRY Run will help you to understand the functioning of the odd or even program in c. The following dice are the user input; you need to determine between them as odd or even.
To learn more about conditional statements, you can check the article below.
Now will discuss the two programs, each having different conditional statements to implement the odd or even program in c. the reason behind implementing the odd or even program in c with different conditional statements is to show that you can use any mathematical statement if it satisfies the odd or even conditions.
Program 1
C
C
#include <stdio.h> int main() { int number;
/* taking the user input*/ printf("enter the number: "); scanf("%d", & number);
/* checking if the number is divisible by 2*/ if (number % 2 == 0) {
/* printing the even number result*/ printf("%d is an even number", number); }
else { /* printing the odd number result*/ printf("%d is an odd number", number); } return 0;
You can use any condition in the if-else statement that satisfies the criteria of odd or even numbers. For example, if you use the (number/2)*2 condition and if this condition is equal to the number variable.
This will be even because the / operator gives the divisor of the numbers. So if you multiply the divisor again by 2, it will equal the number itself, which means it is even.
C
C
#include <stdio.h> int main() { int number;
/* taking the user input*/ printf("enter the number: "); scanf("%d", & number);
/* checking if the number is even or not*/ if ( (number/2)*2 == number) {
/* printing the even number result*/ printf("%d is an even number", number); }
else { /* printing the odd number result*/ printf("%d is an odd number", number); } return 0; }
Program to Check Odd or Even Using the Ternary Operator
The ternary operator (? :) in C is a concise way to make decisions based on conditions. It can also be used to determine if a number is odd or even. Below is the program and its explanation.
C
C
#include <stdio.h>
int main() { int number;
// Input a number from the user printf("Enter an integer: "); scanf("%d", &number);
// Check odd or even using the ternary operator (number % 2 == 0) ? printf("%d is even.\n", number) : printf("%d is odd.\n", number);
How to write program for odd and even numbers in C?
To write a program for odd and even numbers in C, use the modulo operator (%). If the remainder is zero when a number is divided by 2, it's even; otherwise, it's odd.
How to find even or odd function in C?
To find even or odd in C, use a function checking the remainder of the number divided by 2. If it's zero, the number is even; otherwise, it's odd.
How to find odd digit in a number in C?
To find odd digits in a number in C, iterate through each digit using modulo 10. If the digit is odd, print or store it.
How do you know if a number is even or odd in C?
To determine if a number is even or odd in C, use the modulo operator (%). If the result is zero, the number is even; otherwise, it's odd.
Conclusion
In this blog, we explored how to write a C program to check whether a number is odd or even using different methods, including the ternary operator for concise decision-making. Understanding this basic concept is essential for beginners as it lays the foundation for more complex programming challenges.
Check out the articles below to learn more about the C programming language.