Learn how to check odd-even number in C language

In this article, you’ll learn how to check odd-even number in C programming without giving input from the keyboard or giving input from the keyboard.

Look at in the above diagram:

To check odd-even number we generally use modulo operator, we divide the given number with modulo operator and if get remainder == 0 then we say that the given number is even, otherwise, we say that the given number is odd.

 

Example 1: write a program in C language to check a number is odd- even without giving input from keyboard

#include <stdio.h>

#include <conio.h>

int main()

{

int num=36;

if (num%2==0)

printf(“%d is an Even number “,num);

else

printf(“%d is an Odd number”,num);

getch();

}

 

The Output of this program:

36 is an Even number

 

Example 2: write a program in C language to check a number is odd-even, the input is given by the user from the keyboard.

Solution:

#include <stdio.h>

#include <conio.h>

int main()

{

int num;

printf(“Enter an integer number: “);

scanf(“%d”,&num);

if (num%2==0)

printf(“%d is an Even number “,num);

else

printf(“%d is an Odd number”,num);

getch();

}

 

The Output of this program:

Enter an integer number: 95

95 is an Odd number

 

Example 3: Write a C Program to check odd-even number using the ternary operator, without input from the keyboard.

Solution:

#include <stdio.h>

#include <conio.h>

int main()

{

int num=89;

(num%2==0)?printf(“%d is an Even number “,num):

printf(“%d is an Odd number”,num);

getch();

}

 

The output of this program:

89 is an Odd number

Note: Ternary operator must write in a single statement;

Syntax of the ternary operator or conditional operator:

(condition) ? statement 1: statement 2;

Note: if the condition is true then statement 1 is executed and if the condition is false statement 2 is executed.

Example 4: Write a C Program to check odd-even number using ternary operator, with input from the keyboard

Solution:

#include <stdio.h>

#include <conio.h>

int main()

{

int num;

printf(“Enter an integer number:”);

scanf(“%d”,&num);

(num%2==0)?printf(“%d is an Even number “,num):printf(“%d is an Odd number”,num);

getch();

}

 

The output of this program:

Enter an integer number:93

93 is an Odd number

 

Example 4: Write a C Program to check odd-even number without using module (%) operator, without input from the keyboard.

#include <stdio.h>

#include <conio.h>

int main()

{

int num=66;

if (num & 1)

printf(“%d is an Odd number “,num);

else

printf(“%d is an Even number”,num);

getch();

}

 

The output of this program:

66 is an Even number

Note: In the above program we use & (bitwise AND) operator, this operator works on the binary number. When we transform an even number into binary form then its last bit always 0 and binary of number 1 in the last bit is 1 then we get result 0 (false). since 0*1=0 it means else part is executed, on the other hand, if we transform an odd number into binary form then its last digit always 1 and it gives true and if part is executed.

Example 5: Write a C Program to check odd-even number without using module (%) operator, with input from the keyboard.

#include <stdio.h>

#include <conio.h>

int main()

{

int num;

printf(“Enter an integer number: “);

scanf(“%d”,&num);

if (num & 1)

printf(“%d is an Odd number “,num);

else

printf(“%d is an Even number”,num);

getch();

}

 

Enter an integer number: 46

46 is an Even number

Leave a Reply

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

itetrade.com