C program to check whether a given number is odd or even
C program to check whether a given number is odd or even
  • Post author:
  • Reading time:1 mins read
  • Post category:C Programs

Numbers are everywhere checking whether they are even or odd number is a simple problem. Let’s solve it by using c programming.

C program to check whether a given number is odd or even using ternary operator

#include<stdio.h>
int main()
{
 int var1;
 printf("Enter the number\n");
 scanf("%d",&var1);
 (var1 % 2== 0) ? printf("The number %d is even \n",var1) : printf("The number %d is odd  \n",var1);
return 0;
}

Output

C program to check whether the given number is even or odd

Code explanation

First, declare a variable of integer type.

Then using printf() function print “Enter the number”.

Using scanf() read the number entered by the user.

Using ‘%'(modulus operator) divide the number by 2 and compare the result with 0 using the ‘==’ operator.

Then using the ‘?:’ ternary operator the true or false statement is selected. Then using printf() function print the result onto the console.


People also ask for

How do you know if a number is even or odd?

We divide the given number by 2. Then if the remainder is 0 it is even otherwise the number is odd.