Odd or Even Program In C (7 Different Ways)
In this article, we will make an Odd or Even Program In C. In this program first, we take a number from the user, then we will check whether the number entered by the user is Even or Odd.
To find Odd or Even, We’ll make this program in 7 different ways -:
- C Program to Find Even or Odd Using if else or Modulus Operator
- C Program to Check Even or Odd Using Bitwise Operator
- C Program to Check Even or Odd Without Using bitwise or modulus operator
- C Program to Check Even or Odd Using Conditional Operator or Ternary Operator
- C Program to Check Whether a Number is Even or Odd Using Function
- C Program to Check Even or Odd Using Switch Case
- C Program to Check Even or Odd Using goto statement
To make this program, we will use the following concept given below.
- If Else Statement in C Language
- Bitwise Operators in C
- Conditional Operator in C
- Function in C
- Switch Statement in C
- Goto Statement in C
If you do not know about these topics well, then you may not understand this program, so you should know about them in detail from the link given above.
Now without wasting time let’s start programming.
Odd or Even Program In C Using if else or Modulus Operator
Algorithm -:
- Program Start
- Declaration of variable (int x;)
- Input Number From the user
- Assign the value in variable
- Checking condition
- Give answer according to condition
- Program End
Flowchart
Program -:
//C Program to Find Even or Odd Using if else or Modulus Operator
#include<stdio.h>
void main()
{
// variable Declaration
int x;
//input number
printf("Enter a number\n");
scanf("%d",&x);
//Checking Whether a Number is Even or Odd
if(x%2==0) // Modulus (%) returns remainder
printf("Even number");
else
printf("Odd number");
}
Output -:
Enter a number
87
Odd number
C Program to Check Even or Odd Using Bitwise Operator
Program -:
/*write a c program to check even or odd using bitwise operator in c */
#include<stdio.h>
void main()
{
int N;
//input number
printf("Enter a number\n");
scanf("%d",&N);
// If N & 1 is true
if(N&1)
printf("Odd number");
// Otherwise
else
printf("Even number");
}
Output -:
Enter a number
7
Odd number
C Program to Check Whether a Number is Even or Odd Without Using Bitwise or Modulus Operator
Program -:
//C Program to Check Whether a Number is Even or Odd
//Without Using Bitwise or Modulus Operator
#include<stdio.h>
void main()
{
int Num;
//input number
printf("Enter a number\n");
scanf("%d",&Num);
// If (Num/2)*2==Num is true
if((Num/2)*2==Num)
printf("Even number");
// Otherwise
else
printf("Odd number");
}
Output -:
Enter a number
10
Even number
C Program to Check Even or Odd Using Conditional Operator or Ternary Operator
Program -:
//C Program to Check Even or Odd
//Using Ternary Operator or Conditional Operator
#include<stdio.h>
void main()
{
//variable declaration
int num;
//input number
printf("Enter a number\n");
scanf("%d",&num);
//using conditional operator checking odd even
num%2==0?printf("Even number"):printf("Odd number");
}
Output -:
Enter a Number
14
Even number
C Program to Check Whether a Number is Even or Odd Using Function
To make and understand this program made by function, first of all you need to know the basic of the function.
You can read about the function from here -: Function in C
Algorithm -:
- Program Start
- Declaration of variable (int x;)
- Input Number From the user
- Assign the value in variable
- Calling Function to check even or odd
- Check condition
- Give answer according to condition
- Program End
Program -:
//C Program to Check Whether a Number is Even or Odd
//Using Function
#include<stdio.h>
void iseven(int num)
{
if(num%2==0)
printf("Even number");
else
printf("Odd number");
}
void main()
{
int x; //variable declaration
//input number
printf("Enter a number\n");
scanf("%d",&x);
//calling function to check odd even
iseven(x);
}
Output -:
Enter a number
2
Even number
C Program to Check Even or Odd Using Switch Case
Program -:
//C Program to check Even or Odd Using Switch Case Statement
#include<stdio.h>
void main()
{
// variable Declaration
int x,y;
//input number
printf("Enter a number\n");
scanf("%d",&x);
//Checking Whether a Number is Even or Odd
if(x%2==0) // Modulus (%) returns remainder
y = 1;
else
y = 0;
switch(y)
{
case 0: printf("%d is a odd number",x);
break ;
case 1: printf("%d is a even number",x);
break;
}
}
Output -:
Enter a number
6
6 is a even number
C Program to Check Even or Odd Using goto statement
Program -:
//C Program to Find Even or Odd Using goto Statement
#include<stdio.h>
void main()
{
// variable Declaration
int x,y;
//input number
printf("Enter a number\n");
scanf("%d",&x);
//Checking Whether a Number is Even or Odd
if (x%2 == 0)
goto even;
else
goto odd;
even:
printf("%d is a even number\n", x);
exit(0);
odd:
printf("%d is a odd number\n", x);
}
Output -:
Enter a Number
13
13 is a odd number
Read More
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
So friends in this article We will make this program in various ways. These are as follows:
- C Program to Find Even or Odd Using if else or Modulus Operator
- C Program to Check Even or Odd Using Bitwise Operator
- C Program to Check Even or Odd Without Using bitwise or modulus operator
- C Program to Check Even or Odd Using Ternary Operator or Conditional Operator
- C Program to Check Whether a Number is Even or Odd Using Function
- C Program to Check Even or Odd Using Switch Case
- C Program to Check Even or Odd Using goto statement
If you have difficulty to understanding any program or have any doubts or questions, then tell me in the comment below.