# Even Odd Program in C

In this post, you will learn even odd program in the C language. We will write different C programs to check or find even or odd numbers.

## Even Number Program in C

First, you should know how to write a C program to find even numbers. If the number is divisible by 2 then it is even number.

``````#include<stdio.h>
int main()
{
int number;

printf("Enter number: ");
scanf("%d",&number);

if(number%2 == 0)
printf("%d is an even number \n", number);
else
printf("%d is not an even number \n", number);

return 0;
}``````

Output:-

Enter number: 10
10 is an even number

Enter number: 9
9 is not an even number

C program to print even numbers using for loop

If we need to find all the even numbers in a range then we should use a loop. The below program display all even numbers in a range.

``````#include<stdio.h>
int main()
{
int minRange, maxRange;

printf("Enter range value: ");
scanf("%d %d",&minRange, &maxRange);

printf("All even numbers from %d to %d are: \n",
minRange, maxRange);
for(int i=minRange; i<=maxRange; i++)
{
if(i%2 == 0)
printf("%d ", i);
}

return 0;
}``````

Output:-

Enter range value: 10 25
All even numbers from 10 to 25 are:
` 10 12 14 16 18 20 22 24`

Enter range value: 100 110
All even numbers from 100 to 110 are:
` 100 102 104 106 108 110`

## Odd Number Program in C

Now, you should know that how to write a program to check a number is an odd number or not. If the number is not divisible by 2 then the number is an odd number.

``````#include<stdio.h>
int main()
{
int number;

printf("Enter number: ");
scanf("%d",&number);

if(number%2 != 0)
printf("%d is an Odd number \n", number);
else
printf("%d is not an Odd number \n", number);

return 0;
}``````

Output:-

Enter number: 10
10 is not an odd number

Enter number: 9
9 is an odd number

If you need to find all odd number programs within a range then it is similar to the previous C program to print even numbers using for loop. Try it yourself and if you found it difficult then comment in the section.

## Even odd program in C

Up to now, you learned how to write a program to check number is even or not, and then check number is odd or not. If you combine both of the programs then it will a new program which checks the number is even or odd. There are various methods to write this program.

If the number is divisible by 2 then the number is even else the number is odd.

### Even odd program in C using if-else

``````#include<stdio.h>
int main()
{
int number;

printf("Enter a number: ");
scanf("%d",&number);

if(number%2==0)
printf("%d is Even Number\n",number);
else
printf("%d is odd Number\n",number);

return 0;
}``````

Output:-

Enter a number: 9
9 is odd Number

Enter a number: 4
4 is Even Number

When the number is divisible by 2 then if block will be executed otherwise else block will be executed.

Know more:- If-else statement in C, and Programs on if-else in C

### Using the Switch case

``````#include<stdio.h>
int main()
{
int n, rem;

printf("Enter Number: ");
scanf("%d",&n);
rem = n % 2;

switch(rem)
{
case 0:
printf("Even\n");
break;

case 1:
printf("Odd\n");
break;

default:
printf("Invalid");
break;
}

return 0;
}``````

The condition of the switch case statement is the variable rem. The variable rem can be either 1 or 0. Based on the result of variable rem, matched case label statements will be executed. If the result is not matched with the case label then the default statement will be executed.

Know more:- switch case statement in C, and Programs on switch case in C

### Using the Conditional Operator

A conditional operator syntax is (condition)? statement1:statement2

If condition will be true then statement1 executed otherwise statement2.

``````#include<stdio.h>
int main()
{
int n;

printf("Enter a positive Number: ");
scanf("%d",&n);

(n%2==0)?(printf("Even")):(printf("Odd"));

return 0;
}``````

Know more:- conditional operator in C

### Using for loop

When you need to check all numbers within range then for loop, while loop, or do-while loop can be used to solve this problem. Below program check every number in a range.

Know more:- While loop, do-while loop, and For loop

``````#include<stdio.h>
int main()
{
int minRange, maxRange;

printf("Enter range value: ");
scanf("%d %d",&minRange, &maxRange);

for(int i=minRange; i<=maxRange; i++)
{
if(i%2 == 0)
printf("%d: Even\n", i);
else printf("%d: Odd\n", i);
}

return 0;
}``````

Output:-

Enter range value: 10 20
10: Even
11: Odd
12: Even
13: Odd
14: Even
15: Odd
16: Even
17: Odd
18: Even
19: Odd
20: Even

## Even Odd Program in C using function

A user-defined function can be used to solve the problem. A function is a block of code that performs a specific task. In C language a big program divided into several small subroutines/functions/procedures. Learn more:- Introduction to function, user-defined functions

`````` #include<stdio.h>
void checkOddEven(int number)
{
if(number%2 == 0)
printf("%d is an even number \n", number);
else
printf("%d is an odd number \n", number);
}
int main()
{
int number;
printf("Enter number: ");
scanf("%d",&number);

checkOddEven(number);

return 0;
}``````

Output:-

Enter number: 19
19 is an odd number

Enter number: 20
20 is an even number

## Even Odd Program in C without using any operator

In previous every program to check odd-even, we used modulus operator. Now one question can come into your mind that is modulus operator is the only possible way to check odd-even number? The answer to this question is No. The modulus operator is not the only one way to check odd-even number in C.

### Even or odd program without using modulus operator in C

If you want to write a program to find even or odd without using mod or modulus operator in C then the division operator can be used to solve this problem.

``````#include<stdio.h>
int main()
{
int n;

printf("Enter a number:");
scanf("%d",&n);

if((n/2)*2==n)printf("Even");
else printf("Odd");

return 0;
}``````

The expression (n/2)*2 gives the same result as n%2 gives. So, we can place it in the if-else condition. The remaining all other things are the same as the previous program.

### C program to check odd or even using bitwise operator

We can also write a program to check even or odd without using modulus and division in C. For this purpose, the bitwise operator is helpful for us.

The Bitwise AND (&) operator can be used to quickly check if a number is odd or even.

`````` #include<stdio.h>
int main()
{
int number;
printf("Enter number: ");
scanf("%d",&number);

if(number&1) printf("Odd");
else printf("Even");

return 0;
}``````

The value of the expression (x & 1) would be non-zero only if x is odd, otherwise, the value would be zero.

``````#include<stdio.h>
int main()
{
int x=5;
(x&1)?printf("Odd"):printf("Even");
return 0;
}``````

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!