C Program to check whether a number is odd or even


March 26, 2022, Learn eTutorial
1691

An Odd number is defined as a number that is not divisible by 2 and an Even number can be defined as the number that is perfectly divisible by 2. For a better understanding of this odd or even number check C program example, we always recommend you to learn the basic topics of C programming listed below:

What is an odd number and an even number? How to check the number is odd or even??

A natural number can be either an odd number or an even number, This C program is to check whether a number is an odd or even number.

  • Odd Number: An odd number can be defined as the number which is not divisible by the number 2. for example 1,3,5,7,9,...
  • Even Number: An even number is a number that is perfectly divisible by the number 2. for example 2,4,6,8,...

The logic behind this C program is to divide the given number by 2 until it cannot be further divisible, then check for the remainder and see if it is zero or one. if the remainder is zero, it is an even number, if the remainder is not zero then the given number is an odd number.

How do we check odd or even number using the C program

To apply the odd or even number check logic in C language.First, the program accepts the user input using printf and scanf statements.  Now use the "MOD (%) operator" which divides the number with 2 until it cannot be further divisible. Let's take some simple examples to understand more about this.

  1. If the entered number is odd:

    Let the entered value be 'n = 5'. According to the logic, we used in the C program, if '(n%2 = 0)', then a is an Even number otherwise Odd. Here 'n = 5', then we have (5%2 not equal to zero), so the given number is Odd

  2. If the entered number is even:

    Let entered value 'n = 6'. In accordance with the program logic, if (n%2 == 0), then a is an Even number otherwise Odd. Here 'n = 6',then we have (6%2 = 0),so the given number is Even.

 

check whether a number is odd or even

ALGORITHM

STEP 1: Import the header libraries into the C program to use the built-in Functions. 

STEP 2: Start the main() to start the execution of the program,

STEP 3: Initialize the variable n for the user input value using int datatype.

STEP 4: Accept the number from the user using printf and scanf and save that in variable n

STEP 5: Use the mod operator(%) with 2 and Use the if condition to check the remainder is zero or not. if the remainder is zero then print it is an even number, else print it is an odd number.

C Source Code

                                          #include <stdio.h>

void main() {

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

OUTPUT

Enter an integer:13
13, is an odd integer

RUN2

Enter an integer:24
24, is an even integer