C Program to check number even or odd

A simple C program to check whether number is even or odd. In given program, modulus operator is used. Suppose number is 4 then remainder is when we divide this number with 2, is 0. 4%2=0.
C Program to check even or odd using modulus operator
#include<stdio.h>
int main()
{
 int n;
 printf("Enter a number: ");
 scanf("%d",&n);
 if(n%2==0) //remainder==0
 {
  printf("Number is even \n");
 }
 else
 {
  printf("Number is odd \n");
 }
 return 0;
}
C program to check even or odd using conditional operator
#include<stdio.h>
int main()
{
 int n;
 printf("Enter a number: ");
 scanf("%d",&n);
n%2==0?printf("Number is even\n"):printf("Number is odd \n");
 return 0;
}




Popular posts from this blog