C program to check a number even, odd, or zero

In this program, we will test a given number by users whether the number is even, odd, or zero using C programming. To do this we will use the basic component of C syntax. The logic behind this problem is to check the number even or odd, so we know to check logic in C programming, we use basic if-else expressions.

Mathematical logic to Check Even or Odd

By using our general school mathematical knowledge, how can we determine if a number is even or odd. To do this, simply take a number and divided by 2 then notice of its reminder. If the reminder is 1 then again focus on the taken number, it will be a odd number.

And similarly, if the reminder is 0, then obviously it is an even number. See the simple math above. Now we will implement this problem in C program.

Simple logic

First we will take a variable say N to store the given value (simple integers int N) by users. Then we will divide it by 2 and check the reminder using the C modulus operator N%2 == 0 or N%2 == 1. After then we will check the reminder by using simple if.. else conditions. Finally output it as the sample input output above. Let’s implement it.

To take input value from users, use scanf function and to display the output we will use printf function which is defined in the stdio.h standard function library along with many other basic function and expressions.

If you are very beginner in C programming, then you can start learning our basic C tutorial,

Program to Check Even or Odd Numbers

/* C program to check Even or Odd (main.c)*/
#include <stdio.h>
int main() {
    int N;
    printf("Enter a number: ");
    scanf("%d", &N);
    
    if (N == 0) {
        printf("You entered Zero!\n");
    }
    else if (N % 2 == 0) {
        printf("You entered an Even number!\n");
    }
    else {
        printf("You entered an Odd number!\n");
    }
    return 0;
}
Sample Input Sample Output
22 You entered an Even number!
45 You entered an Odd number!
0 You entered Zero!

🙂  Easy Coding