New update is available. Click here to update.

# C program to check whether the number is odd or even

dhananjay kumar
Last Updated: Mar 23, 2023
EASY

## Introduction

In this blog, we will discuss how to implement the odd or even program in c. first; we will try to understand the problem statement so that we can implement the program efficiently.

Once we are done with the problem statement, we will focus on the DRY run to understand the program's working.

But before we implement the program, let's discuss C programming.

Also see, Binary to Hex Converter

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

## Problem Statement

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.

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.

## Dry Run

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.

Conditional statements

## Implementation

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

``````#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;

``````

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

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

``````#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;
}``````

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

## Frequently Asked Questions

### C is which type of language?

C is a mid-level language that can be used to implement system and application programming.

### Can I run my c program on different platforms or machines?

C programming language is platform-independent so that you can run your program on any system.

### Do I need to learn the C language as a beginner?

C language is considered foundational, but you can learn any language you want.

### Is C programming language a procedural language?

Yes, C is one of the oldest procedural programming languages.

### What does the modulus(%) of numbers return?

The modulus of two numbers returns the remainder of the numerator.

## Conclusion

In this blog, we learned how to differentiate between odd and even numbers. We have also learned how to implement the odd or even program in C. We have discussed conditional statements.

Check out the articles below to learn more about the C programming language.

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Codestudio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

Happy Coding!