Example:
Input any number: 10
Output: Even
Required knowledge
Basic C programming, FunctionsAs I have already explained in many of my earlier posts how to check the even/odd conditions whether using if else statements or using switch case or using conditional operators or using bitwise operators. You can apply any of the logic with the concept of functions to check even or odd using functions. Here I am using concept of bitwise operators to find even or odd, as its the fastest method of checking even/odd.
Program to check even or odd
/** * C program to check even or odd using functions */ #include <stdio.h> /** * Checks whether the least significant bit of the number is 1 or not. If it is * 1 then the number is odd else even. */ int isOdd(int num) { return (num & 1); } int main() { int num; /* * Reads a number from user */ printf("Enter any number: "); scanf("%d", &num); /* If isOdd() function returns 1 then the number is odd */ if(isOdd(num)) { printf("The number is odd."); } else { printf("The number is even."); } return 0; }
Output
Enter any number: 22
The number is even.
The number is even.
Happy coding ;)
You may also like
- Function and recursion programming exercises index.
- C program to find cube of any number using functions.
- C program to find maximum or minimum between two numbers using functions.
- C program to check prime, strong, armstrong or perfect numbers using functions.
- C program to print all armstrong numbers between 1 to n using functions.
- C program to find power of any number using recursion.
- C program to find sum of digits using recursion.
- C program to generate nth fibonacci series using recursion.
- C program to find GCD of any two numbers using recursion.