Odd/Even Program in C

Program Description

Odd/Even determination for a given number.


This is a very basic program that shows, how to determine whether the given number is even or odd in C language. The program simply prompts the user for a number, and displays whether the given number is odd or even.

The program uses a basic mathematical property, that every even number is completely divisible by 2. If a number is not completely divisible by 2,it is an odd number. Here we've used the modulo operator (%) to find the remainder after dividing the number by 2.


It's a very simple program, but using the modulo(%) trick, is very helpful in various, not so obvious problems.

Program
/***********************************
*  Odd/Even Program in C
*	
*	code.cheraus.com
*
***********************************/

#include<stdio.h>

int main()
{
  int no;
  
  printf("\nEnter the Number : ");
  scanf("%d",&no);
  
  if(no%2==1)   //if number if completely divisible(ie. remainder is 0 after dividing by 2)
   printf("\nThe number is odd\n");
   
  else
  printf("\nThe number is even\n");
  
  return 0;
  
}

Output
Test Case 1:

Enter the Number : 23

The number is odd

Test Case 2:


Enter the Number : 22

The number is even

Test Case 3:


Enter the Number : 2

The number is even

Comments
comments powered by Disqus