A directory of Objective Type Questions covering all the Computer Science subjects. Here you can access and discuss Multiple choice questions and answers for various compitative exams and interviews.

C program to check if number is odd or even

Problem Description

This program is to check whether the number provided by the user is even or odd. As you aware that even numbers are completely divisible by zero i;e they leave remainder zero when divided by two. On the other hand, odd numbers are not completely divisible by two. So, we can make use of the remainder operator(%) in c to check whether a number is odd or even.

C program to check if number is odd or even - Source code
  /* Program to check whether the number
  is even or odd using the modulus operator (%)

int main()
   int n;

   printf("Enter an integer: ");

   // Modulus (%) returns remainder
   if ( n%2 == 0 )   // returns true if remainder is 0
      printf("%d is an even number", n);
      printf("%d is an odd number", n);

   return 0;

Program Output

Case 1:

Enter an integer: 6723
6723 is an odd number

Case 2:

Enter an integer: 4578
4578 is an even number

Program Explanation

Modulus operator in C programming language, denoted by %, returns the remainder after the division operation. Here we have perform the modulus operator on the number provided by the user and checked whether it is equal to zero(n%2 == 0). The if statement is executed automatically if the expression inside it returns true value. Otherwise, the else statement is executed.