In this example, you will learn to check whether a number entered by the user is even or odd.
An even number is an integer that is exactly divisible by 2. For example: 0, 8, -24
An odd number is an integer that is not exactly divisible by 2. For example: 1, 7, -11, 15
Program to Check Even or Odd
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
Out Put
Enter an integer: -7
-7 is odd.
In the program, the integer entered by the user is stored in the variable num.
Then, whether num is perfectly divisible by 2
or not is checked using the modulus %
operator.
Conclusion:
Congratulations! You have successfully written a C program that checks whether a given number is even or odd. By following this step-by-step guide, you have gained valuable knowledge of the C language and improved your programming skills. Keep practicing, exploring different concepts, and challenging yourself to become a proficient programmer.