Example:
Input number: 10
Output: Even number
Also view this program using -
C program to check even or odd using if else.
C program to check even or odd using switch case.
C program to check even or odd number using function.
Required knowledge
Basic C programming, Conditional operatorEven numbers
Even numbers are the positive integers that are exactly divisible by 2. For example: First 5 even numbers are - 2, 4, 6, 8, 10...Odd numbers
Odd numbers are the positive integers that are not exactly divisible by 2. For example: First 5 odd numbers are - 1, 3, 5, 7, 9...Program to check even or odd
/**
* C program to check even or odd number using conditional operator
*/
#include <stdio.h>
int main()
{
int num;
/*
* Reads a number from user
*/
printf("Enter any number to check even or odd: ");
scanf("%d", &num);
/* if(n%2==0) then it is even */
(num%2 == 0) ? printf("The number is EVEN") : printf("The number is ODD");
return 0;
}
Note: We can also write same program using conditional operator as:
/**
* C program to check even or odd number using conditional operator
*/
#include <stdio.h>
int main()
{
int num;
/*
* Reads a number from user
*/
printf("Enter any number to check even or odd: ");
scanf("%d", &num);
/* Print Even if (n%2==0) */
printf("The number is %s", (n%2==0 ? "EVEN" : "ODD"));
return 0;
}
Output
Enter any number to check even or odd: 20
The number is EVEN
The number is EVEN
Happy coding ;)
You may also like
- Conditional operator programming exercises index.
- C program to find maximum between two numbers using conditional operator.
- C program to find maximum between three numbers using conditional operator.
- C program to create simple Calculator using switch case.
- C program to enter P, T, R and calculate Simple Interest.
- C program to enter P, T, R and calculate Compound Interest.
- C program to convert temperature from Celsius to Fahrenheit.
- C program to print all alphabets from a to z.
- C program to print sum of all even numbers from 1 to n.
- C program to calculate sum of digits of any number.
- C program to find factorial of a number.