Table of Contents
C Check whether the number is odd or even using operators
In this tutorial, we will discuss the Program to C Check whether the number is odd or even using operators
In this post, we are going to learn how to check whether the given number is odd or even number using operator in C language
C Check whether the number is odd or even
Check whether the number is odd or even – using modular operator
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int num;//variable declaration printf("Enter a integer to check odd or even\n"); //the program Asks input from the user scanf("%d",&num); //the integer entered by the user and it is stored in variable num if(num%2==0){ printf("%d is even",num); } else{printf("%d is even",num);} getch(); return 0; }
When the above code is executed ,it produces the following result
case 1
Enter a integer to check odd or even 3500 3500 is even
case 2
Enter a integer to check odd or even 2345 2345 is odd
Check whether the number is odd or even – using divisional operator
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int num;//variable declaration printf("Enter a integer to check odd or even\n"); //the program Asks input from the user scanf("%d",&num); //the integer entered by the user and it is stored in variable num if((num/2)*2==num){ printf("%d is even number",num); } else{printf("%d is odd number",num);} getch(); return 0; }
When the above code is executed ,it produces the following result
case 1
Enter a integer to check odd or even 3506 3506 is even number
case 2
Enter a integer to check odd or even 999 999 is odd number
Check whether the number is odd or even – using ternary operator
Program 3
#include <stdio.h> #include <stdlib.h> int main() { int num;//variable declaration printf("Enter a integer to check odd or even\n"); //the program Asks input from the user scanf("%d",&num); //the integer entered by the user and it is stored in variable num (num%2==0)? printf("%d is even",num):printf("%d is odd",num); getch(); return 0; }
When the above code is executed ,it produces the following result
case 1
Enter a integer to check odd or even 5678 5678 is even
case 2
Enter a integer to check odd or even 6767 6767 is odd
Check whether the number is odd or even – using bit wise operator
Program 4
#include <stdio.h> #include <stdlib.h> int main() { int num;//variable declaration printf("Enter a integer to check odd or even\n"); //the program Asks input from the user scanf("%d",&num); //the integer entered by the user and it is stored in variable num if((num & 1)==0){ printf("%d is even number",num); } else{printf("%d is odd number",num);} getch(); return 0; }
When the above code is executed ,it produces the following result
case 1
Enter a integer to check odd or even 4 4 is even number
case 2
Enter a integer to check odd or even 5 5 is odd number
Check whether the number is odd or even – using shift operator
Program 5
#include <stdio.h> #include <stdlib.h> int main() { int num;//variable declaration printf("Enter a number to check even or odd\n"); //the program Asks input from the user scanf("%d",&num); //the integer entered by the user and it is stored in variable num if((num>>1)<<1==num) printf("%d is Even number",num); else printf("%d is Odd number",num); getch(); return 0; }
When the above code is executed ,it produces the following result
Case 1
Enter a number to check even or odd 4321 4321 is Odd number
Case 2
Enter a number to check even or odd 4326 4326 is Even number
Suggested for you
Data type in C language
Variable in C language
Similar post
Java Program to check whether the number is odd or even using operators
C Program to check whether the number is odd or even using operators
C++ Program to check whether the number is odd or even using operators
Python Program to check whether the number is odd or even using operators