dhairyashah
Portfolio

Aug 28th, 2023

C Program to check if the number is Even or Odd

Author Picture

Dhairya Shah

Software Engineer

We will use the following methods to determine whether the number is Even or Odd in C:

Program to check Even or Odd (if…else)

#include<stdio.h>

int main(){
	int n;
	printf("Enter a number: ");
	scanf("%d", &n);
	
	if(n % 2 == 0){
		printf("%d is an even number", n);
	}else{
		printf("%d is an odd number", n);
	}
}

Program to check Even or Odd (Ternary Operators)

#include <stdio.h>

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);

    n % 2 == 0 ? printf("%d is an even number", n) : printf("%d is an odd number", n);

    return 0;
}

In the above program, we have used the ternary operator ?: instead of the if...else statement.

This article was helpful to you. Thanks for reading!