Skip to main content

To Check Whether a Number is Even or Odd


C Program to Check Whether a Number is Even or Odd

Numbers perfectly divisible by 2 are known even numbers and numbers which are not divisible by 2 are called odd numbers.
This program takes an integer from user and checks whether that number is even or odd and displays the result.

Source Code


/*to check whether a number entered by user is even or odd. */

#include <stdio.h> 
  int main()
{
      int num;
      printf("Enter an integer you want to check: ");
      scanf("%d",&num);
      if((num%2)==0)      /* Checking whether remainder is 0 or not. */
           printf("%d is even.",num);
      else
           printf("%d is odd.",num);
      return 0;
}
Output 1

Enter an integer you want to check: 25
25 is odd.
Output 2

Enter an integer you want to check: 12
12 is even.
 
In this program, user is asked to enter an integer which is stored in variable num. Then, the remainder is found when that number is divided by 2 and checked whether remainder is 0 or not. If remainder is 0 then, that number is even otherwise that number is odd. This task is performed using if...else statement in C programming and the result is displayed accordingly.
This program also can be solved using conditional operator[ ?: ] which is the shorthand notation for if...else statement.


/* to check whether an integer is odd or even using conditional operator */
 
#include <stdio.h>  
  int main()
{
    int num;
    printf("Enter an integer you want to check: ");
    scanf("%d",&num);
    ((num%2)==0) ? printf("%d is even.",num) : printf("%d is odd.",num);
    return 0;
} 
 

 
 

Comments

Popular posts from this blog

C Program to print the pattern

C Program to print the pattern X X Y X Y Z ........................................................................................ #include<stdio.h> #include<conio.h> void main () { int i , j ; for ( i= 88 ; i< 91 ;i ++) { for ( j = 88 ;j <= i ; j ++) { printf ( "%c" , j );                    }           printf ( "\n" ); } getch (); } ........................................................................................ OUTPUT

Deen Dayal Upadhyay Gorakhpur University RESULTS

DDU Gorakhpur results chahiye to yaha click kare                                                                       RESULTS  

RESULT DDU Gorakhpur 2016

Deen Dayal Upadhyay Gorakhpur University   Check Your Result click Here  B. Sc. Part-II 26-MAY-2016   M.Sc.(Ag.) Genetics & Plant Breeding (Final) 26-MAY-2016   M.Sc. ( Agriculture ) (Agricultural Economics ) - I 26-MAY-2016   B.A. Part-II 26-MAY-2016   B.A. Part-I 24-MAY-2016   B.A. Part-III 16-MAY-2016   B.Com. Part-III 16-MAY-2016   B.Sc. Home Science-I 16-MAY-2016   B.Sc. Home Science-II 16-MAY-2016   B.Sc. Home Science-III 16-MAY-2016   M.A Economics-Final 16-MAY-2016   M.A Hindi-Prev 16-MAY-2016   M.A Hindi-Final 16-MAY-2016   M.A History-Prev 16-MAY-2016   M.A. Ancient History, Archaeology and Culture (Previous) 12-MAY-2016   M.A. Ancient History, Archaeology and Culture (Final) 12-MAY-2016   B.Com.- I Year 12-MAY-201...