In this tutorial, we will discuss a simple concept of C program to check whether a number is even or odd
In this program, we are going to learn about how to find odd or even number from given number using if else statements in C programming language
what is even or odd
When the number is exactly divisible by 2, it is called as the even number eg – 2,4,6,8,10
if the number is not exactly divisible by 2, that is called the odd number – 1,3,5,7,9
Once This C program received the number it will check the given number either odd or even.
After the program received input from the user, it is stored in the variable of num. then program divides the value of num into 2 and displays the output
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int num; printf("Enter a integer value: "); scanf("%d",&num); //get input from the user and store the value in num variable. if(num%2==0)//if the number is divided by 2, the number is even printf("%d is a even number",num); else//otherwise the number is odd printf("%d is a odd number",num); getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter an integer value: 34 34 is a even number
case 2
Enter an integer value: 65 65 is a odd number
when we use the modular operator, if it is the remainder appears as 0, it is called even number and if the remainder displays the balance as one, it is called as odd or uneven number
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int num; printf("Enter an integer number: "); scanf("%d",&num); (num%2==0) ? printf("%d is an even",num): printf("%d is an odd",num); getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter an integer number: 34 34 is an even
case 2
Enter an integer value: 65 65 is an odd number
Program 3
#include <stdio.h> #include <stdlib.h> int main() { int num; printf("Enter an integer number: "); scanf("%d",&num); if(num&1) printf("%d is a odd number",num); else printf("%d is a even number",num); getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter an integer number: 96 96 is an even
case 2
Enter an integer value: 27 27 is an odd number
program 4
#include <stdio.h> #include <stdlib.h> int main() { int num; printf("Enter an integer number: "); scanf("%d",&num); if((num/2)*2==num) printf("%d is a even number",num); else printf("%d is a odd number",num); return 0; }
When the above code is executed, it produces the following results
case 1
Enter an integer number: 54 54 is an even number
case 2
Enter an integer value: 39 39 is an odd number
#include <stdio.h> #include <stdlib.h> int main() { int num; printf("Enter the number: "); scanf("%d",&num); if(((num>>1)<<1)==num ){ printf("%d is even",num); } else{ printf("%d is odd",num); } getch(); return 0; }
When the above code is executed, it produces the following results
case 1
Enter an integer number: 125 125 is odd
case 2
Enter an integer value: 200 200 is eve
The same program in other languages
C++ program to check whether a number is even or odd
Java program to check whether a number is even or odd
Python program to check whether a number is even or odd
Suggested for you
scanf() printf() function in C language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…