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:
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.
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.
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.
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
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.
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.
#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);
}
Enter an integer:13 13, is an odd integer RUN2 Enter an integer:24 24, is an even integer