Table of contents
1.
Introduction
2.
What is C Language?
2.1.
Features of C
3.
Program to Check Even or Odd
3.1.
Example
3.1.1.
Sample Input
3.1.2.
Sample Output
3.1.3.
Explanation
3.2.
Approach
4.
Dry Run Approach
5.
Implementation of odd or even program in c
5.1.
Program 1
5.2.
C
5.2.1.
Output
5.2.2.
Complexity
5.3.
Program 2
5.4.
C
5.4.1.
Output
5.4.2.
Complexity
6.
Program to Check Odd or Even Using the Ternary Operator
6.1.
C
7.
Frequently Asked Questions
7.1.
How to write program for odd and even numbers in C?
7.2.
How to find even or odd function in C?
7.3.
How to find odd digit in a number in C?
7.4.
How do you know if a number is even or odd in C?
8.
Conclusion
Last Updated: Jan 22, 2025
Easy

C program to check whether the number is odd or even

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

C Program to Check Whether the Number is Odd or Even

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.

Also see, C Static Function.

Program to Check Even or Odd

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 remainderif 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. 

You can also read about dynamic array in c, And Tribonacci Series

Dry Run Approach

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. 

user input
dry run


To learn more about conditional statements, you can check the article below.

Conditional statements

Implementation of odd or even program in c

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 also try this code with Online C Compiler
Run Code

Output

program output

Complexity

Now, we will discuss the time complexity of the odd or even program in c.

Time complexity: The time complexity will be O(1) because we execute every program statement in O(1) times.
 

Space Complexity: The space complexity will also be O(1) because we are using only variables to store the user input.
 

Now let's discuss the implementation of the odd or even program in c.

  • First, we take the input from the user with the scanf( ) method and store the input in the variable named number.
     
  • After this, we apply the if-else statement, and in the if statement, we check whether the number%2==0 is true.
     
  • If the statement is true, the given number is even; otherwise, it will jump to the else statement, which prints that the number is odd.

Also read reverse a number.

Program 2

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;
}
You can also try this code with Online C Compiler
Run Code

Output

program 2 output

Complexity

Now, we will discuss the time complexity of the odd or even program in c.

Time complexity: The time complexity will be O(1) because we execute every program statement in O(1) times.
 

Space Complexity: The space complexity will also be O(1) because we are using only variables to store the user input.

Read More - Short int in C Programming

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);

return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Enter an integer: 7

7 is odd.

Frequently Asked Questions

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.

Live masterclass