C program to check a number is even or odd; Through this tutorial, we will learn how to find a number is even or odd in the c program with the help of function, modules operator, and ternary operator.
C Program to Check Whether a Number is Even or Odd
- Algorithm to Check Whether a Number is Even or Odd
- C Program to Check Even or Odd
- C Program to Check Whether a Number is Even or Odd using Function
- C Program to Check Whether a Number is Even or Odd using Ternary Operator
Algorithm to Check Whether a Number is Even or Odd
Use the following algorithm to write a program to check whether a number is even or odd; as follows:
- Step 1: Start Program
- Step 2: Read the number from user and store it in a.
- Step 3: Find the number is even or odd using a % 2 == 0.
- Step 4: Print number is even or odd
- Step 5: Stop Program
C Program to Check Even or Odd
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
The output of the above c program; as follows:
Enter an integer: 10 10 is even.
C Program to Check Whether a Number is Even or Odd using Function
#include <stdio.h>
#include <stdlib.h>
int find_Num(int);//function prototype
int main()
{
int num;
printf("Enter a number to check odd or even :- ");
scanf("%d",&num);
find_Num(num);//calling the function
return 0;
}
//create function
int find_Num(int num){//function definition
if(num%2==0){
printf("\n%d is an even number",num);
}
else{
printf("\n%d is an odd number",num);
}
}
The output of the above c program; as follows:
Enter a number to check odd or even :- 11 11 is an odd number
C Program to Check Whether a Number is Even or Odd using Ternary Operator
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num);
return 0;
}
The output of the above c program; as follows:
Enter an integer: 15 15 is odd.