Check ODD and EVEN
Table of Contents
In this tutorial, we will discuss the C program to check whether a number is even or odd|7 ways
In this post, we are going to learn how to check whether the given numbers is even or odd using different 7 ways in C language
Program 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num=78;
if(num%2==0){
printf("%d is even\n",num);
}
else{
printf("%d is odd\n",num);
}
getch();
return 0;
}
When the above code is executed, it produces the following result
78 is even
In this program,
Program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;//declare the variable
printf("Enter a number: ");
scanf("%d",&num);//read the input from the user
if(num%2==0){
printf("%d is an even\n",num);
}
else{
printf("%d is an odd\n",num);
}
getch();
return 0;
} When the above code is executed, it produces the following result
Case 1
Enter a number: 456 456 is an even
Case 2
Enter a number: 125 125 is an odd
Approach
Program 3
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
printf("Enter a integer number: ");
scanf("%d",&num);//read the input from the user
//using division operator
if((num/2)*2==num){
printf("%d is an Even number",num);
}else{
printf("%d is an Odd number",num);
}
getch();
return 0;
}
When the above code is executed, it produces the following result
case 1
Enter a integer number :456 456 is an even number
case 2
Enter a integer number :451 451 is an odd number
Approach
Program 4
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
/* int num1=450;
int num2=561;*/
printf("Enter any number to check odd or even\n");
scanf("%d",&num);//read the input from the user
switch(num%2){
case 0:
printf("%d is a even number",num);
break;
case 1:
printf("%d is a odd number",num);
}
getch();
return 0;
} When the above code is executed, it produces the following result
case 1
Enter any number to check odd or even 9876 9876 is an even number
case 2
Enter any number to check odd or even 987 987 is an odd number
Approach
Program 5
#include <stdio.h>
#include <stdlib.h>
int findOddeven(int);//function prototype
int main()
{
int num;
printf("Enter a integer number: ");
scanf("%d",&num);
//read input from the user
findOddeven(num);//function call
getch();
return 0;
}
findOddeven(int num){//function definition
if((num%2)==0){
printf("%d is a Even number",num);
}else{
printf("%d is a Odd number",num);
}
}
When the above code is executed, it produces the following result
case 1
Enter a integer number :147 147 is an odd number
case 2
Enter a integer number :346 346 is an even number
Approach
Program 6
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,*ptr;//declare the pointer and normal variable
printf("Enter number to check for odd/even\n");
scanf("%d",&num);//read input from the user
ptr=#//assign address of num variable to pointer variable
if(*ptr %2 ==0)//check value for even
printf("%d is an even",num);
else
printf("%d is an odd",num);
getch();
//printf("Hello world!\n");
return 0;
} When the above code is executed, it produces the following result
Case 1
Enter number for check odd/even 456 456 is an even
Case 2
Enter number for check odd/even 457 456 is an odd
Approach
Program 7
#include <stdio.h>
#include <stdlib.h>
int findOddeven(int);
int main()
{
int num;
printf("Enter a integer number: ");
scanf("%d",&num);
//read input from the user
if(findOddeven(num)){
printf("%d is an Even number",num);
}else{
printf("%d is an Odd number",num);
}
getch();
return 0;
}
int findOddeven(int num){//function definition
if(num==0){
return 1;
}else if(num==1){
return 0;
}
else if(num<0){
return findOddeven(-num);
}
else{
return findOddeven(num-2);
}
}
When the above code is executed, it produces the following result
case 1
Enter a integer number: 1234 1234 is an even number
case 2
Enter a integer number: 1237 1237 is an odd number
Similar post
Python program to check whether a number is even or odd
Java program to check whether a number is even or odd
C++ program to check whether a number is even or odd
Suggested for you
Data type in C language
Variable in C language
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.