How to Write a C Program to Check Odd or Even numbers using If Statement and Conditional Operator with an Example of Each? If a number is divisible by 2, it is an even number, and the remaining (not divisible by 2) are odd numbers.
C Program to Check Odd or Even using IF Condition
This program allows the user to enter an integer. Next, this C program shows how to find or check even and odd numbers using the If statement.
In C Programming, we have an Arithmetic Operator called % (Module) to check the remainder of the division. Let’s use this operator to find the remainder. If the remainder is 0, then the number is even else odd.
Within this C example, the printf statement will ask the user to Enter a value to check whether it is Even or Odd. The scanf statement will assign the user entered value to a number variable.
In the Next line, We declared the If statement. Please refer to the Arithmetic Operator in C Programming article. If condition checks whether the remainder of the number divided by 2 is exactly equal to 0 or not.
- If the condition is True, it is an Even number.
- If the condition is False, it is Odd.
// using If Else #include<stdio.h> int main() { int number; printf(" Enter an int value to check \n"); scanf("%d", &number); if ( number%2 == 0 ) //Check whether remainder is 0 or not printf("Given number %d is EVEN NUMBER \n", number); else printf("Given number %d is ODD NUMBER \n", number); return 0; }
Enter the second value to check for the even number
Enter an int value to check Even or Odd
98
Given number 98 is EVEN NUMBER
In the above code, we used if condition to check whether the remainder of the given integer is 0 or not and print as per the result.
C Program to Check Even or Odd using Conditional Operator
We can also use the Conditional operator to check whether the number 24 is odd or even in this C programming. Let’s see the example program of this.
// using Conditional Operator #include<stdio.h> int main() { int number; printf(" Enter an int value to check \n "); scanf("%d",&number); number%2 == 0 ? printf(" Even \n ") : printf(" Odd \n"); return 0; }
Try other values too
Enter an int value to check
19
Odd