In this tutorial, we are going to write a C Program to check whether a given number is odd or even number.
Any number which is when divided by 2 gives 0 as its remainder is called an even number. Whereas, odd numbers leave 1 as the remainder when divided by 2.
Problem statement:
We need to check if any number is even or odd, by using if-else statements. Here our program will take integer input from the user at runtime.
It should print “Even Number” in case the number is even. And if the number is odd it shoud print “Odd Number”.
Program to Check Given Number is Even Or Odd In C
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num % 2 == 0)
{
printf("%d is Even Number", num);
}
else
{
printf("%d is Odd Number", num);
}
return 0;
}
Example 1:
Input: 10
Output: Even Number
Example 2:
Input: 15
Output: Even Number
Program Explanation
Let’s go through the program step by step to understand it better:
We have included stdio.h to enable the program to do input and output operations.
The main() function is the entry point of any C program.
Here, we have declared the variable ‘num’ of integer type to store user input.
printf() function is used to show the output/message on the display.
scanf() function is used to read the input given by the user.
The % is known as the percentile operator, used to find the remainder when a number is divided by 2.
Inside the if statement we check whether the remainder of ‘num’ when being divided by 2 is equal to 0 or not.
If it divides completely, then the program prints “Even Number”, else it prints “Odd Number”.
The return statement returns 0 to the operating system and denotes the end of a program.
Conclusion
We have learned to write a C program to check whether a given number is odd or even. We have used conditional if-else statements to achieve this.I hope this blog was helpful to you in understanding the C program.