C Program to Check Whether a Given Number is Even or Odd

«
»

This is a C program to check whether a given integer is odd or even.

Problem Description

The program takes the given integer and checks whether the integer is odd or even.

Problem Solution

1. Take the integer to be checked as input.
2. Find the remainder of the integer by dividing it by 2.
3. Use if,else statement to check whether the remainder is equal to zero or not.
4. Print the output and exit.

advertisement
Program/Source Code

Here is source code of the C program to check whether a given integer is odd or even.The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2.  
  3. void main()
  4. {
  5.     int ival, remainder;
  6.  
  7.     printf("Enter an integer : ");
  8.     scanf("%d", &ival);
  9.     remainder = ival % 2;
  10.     if (remainder == 0)
  11.         printf("%d is an even integer\n", ival);
  12.     else
  13.         printf("%d is an odd integer\n", ival);
  14. }
Program Explanation

1. User must first enter the integer to be checked which is stored in the variable ival.
2. Find the remainder of the integer by dividing the variable ival by integer 2 and the value is stored in the variable remainder.
3. Use if,else statement to check whether the value of the variable remainder is equal to zero or not.
4. If it is equal to zero, then print the output as “the integer is an even integer”.
5. If it is not equal to zero, then print the output as “the integer is an odd integer”.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter an integer : 24
24 is an even integer
 
Case 2:
Enter an integer : 75
75 is an odd integer
 
Case 3:
Enter an integer : 0
0 is an even integer

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.