Write a Program to Check Even or Odd Numbers in C Using Function
In this tutorial, we are going to see how to write a program to check even or odd numbers in C language using function. Considering we have an integer and we need to check if it is even or odd using a C program.
There are four ways to check even or odd numbers in C, by using for loop, while loop, if-else, or by creating a function.
An even number is an integer exactly divisible by 2. Example: 0, 4, 8, etc.
An odd number is an integer that is not exactly divisible by 2. Example: 1, 3, 7, 15, etc.
To check if the given number is even or odd, we check the remainder of the division by dividing the number by 2.
Write a Program to Check Even or Odd Numbers in C Using Function
#include <stdio.h> // function to check even or odd numbers int isEven(int n){ if(n%2==0){ printf("\n%d is an even number",n); } else{ printf("\n%d is an odd number",n); } } int main() { int n; printf("Enter a number: "); scanf("%d",&n); isEven(n); //calling the function return 0; }
Output:
Enter a number: 2 2 is an even number