The even-odd program in C helps determine if a given number is even or odd. It categorizes integers based on their divisibility by 2, a common way to distinguish between even and odd numbers.

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

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;

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.

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

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.

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 learned learned how to implement the odd or even program in C.

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