C Program to Check the Given Number is Odd or Even

This article will look at C programs that use several techniques to determine whether the number is odd or even. Even numbers are exactly divisible by two in the decimal number system, whereas odd numbers are not. The modulus operator "%" returns the remainder when used, for instance, 4%3 = 1. (remainder when four is divided by three).

Odd Even Program Explanation:

A number is considered even if it may be divided evenly by 2. Odd numbers are the leftover numbers that are not exactly divisible by 2. To put it simply, odd numbers are those that have the form of n = 2k+1 whereas even numbers take the form of n = 2k. Either even or odd numbers will make up each and every integer. This blog will explain how to use a C program to determine if a number is even or odd.

Odd Even Program Algorithm:

Now let us look at the algorithm for Program to Check Even or Odd

  • Step 1- Start the program.
  • Step 2- Read/input the number.
  • Step 3- if n%2==0 then the number is even.
  • Step 4- else number is odd.
  • Step 5- display the output.
  • Step 6- Stop the program.

Odd & Even Program Pseudocode

IF (integer modulo 2) equals to 0
   PRINT number is even
ELSE
   PRINT number is odd
END IF

C Program Code for Odd Even Number

There are several methods we may use to determine if a given number is even or odd.

Method 1: Odd-even program in C using the modulus operator

This method displays the C program to check the odd-even number using the modulus operator.

#include <stdio.h>
int main()
{
  int n;
  printf("Enter an integer\n");
  scanf("%d", &n);
  if (n%2 == 0)
    printf("Even\n");
  else
    printf("Odd\n");
  return 0;
}

Method 2: C program to find odd or even using a bitwise operator

We can use the bitwise AND (&) operator to check odd or even. For example, consider the binary of 7 (0111), (7 & 1 = 1). You may observe that the least significant bit of every odd number is 1. Therefore (odd_number & 1) is one always and also (even_number & 1) is always zero.

#include <stdio.h>
int main()
{
  int n;   
  printf("Input an integer\n");
  scanf("%d", &n);
  if (n & 1 == 1)
    printf("Odd\n");
  else
    printf("Even\n");   
  return 0;
}

Method 3: Even odd programs in C using conditional operator

This method describes the even-odd program in C using a conditional operator.

#include <stdio.h>
int main()
{
  int n;
  printf("Input an integer\n");
  scanf("%d", &n);
  n%2 == 0 ? printf("Even\n") : printf("Odd\n");
  return 0;
}

Now that you’ve been through the above programs, you should understand how to use C programming to determine if a given integer is odd or even. I hope you find our site to be useful and helpful.
This concludes our discussion of the Odd & Even Program in C.

Other C Programs

C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Selection Sort Program in C

Leave a Reply

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