C

Write a Program to Check Even or Odd Numbers in C Using if-else

In this tutorial, we are going to see how to write a program to check even or odd numbers in C language using if-else. Considering we have an integer and we need to check if it is even or odd using a C program.

There are four ways to check even or odd numbers in C, by using for loop, while loop, if-else, or by creating a function.

An even number is an integer exactly divisible by 2. Example: 0, 4, 8, etc.

An odd number is an integer that is not exactly divisible by 2. Example: 1, 3, 7, 15, etc.

To check if the given number is even or odd, we check the remainder of the division by dividing the number by 2.
 

Write a Program to Check Even or Odd Numbers in C Using if-else With the Modulo Operator
#include <stdio.h>
 
int main()
{
   int nbr;
   
   printf("Enter an integer number:\n");
   scanf("%d", &nbr);
   
   if (nbr % 2 == 0)
      printf("Even\n");
   else
      printf("Odd\n");
     
   return 0;
}

Output:

Enter an integer number:
2
Even

 

 

Write a Program to Check Even or Odd Numbers in C By using the Ternary Condition
#include <stdio.h>
 
int main()
{
   int nbr;
   
   printf("Enter an integer number:\n");
   scanf("%d", &nbr);
 
   nbr % 2 == 0 ? printf("Even\n") : printf("Odd\n");
     
   return 0;
}

Output:

Enter an integer number:
2
Even

 

 

Write a Program to Check Even or Odd Numbers in C Using if-else With the Bitwise operator
#include <stdio.h>
 
int main()
{
   int nbr;
   
   printf("Enter an integer number:\n");
   scanf("%d", &nbr);
   
   if (nbr & 1 == 1)
      printf("Odd\n");
   else
      printf("Even\n");
    
   return 0;
}

Output:

Enter an integer number:
2
Even

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *