C program to check odd or even using recursion
- Home
- Find elements
- C program to check odd or even using recursion
- On
- By
- 0 Comment
- Categories: Find elements
C program to check odd or even using recursion
C program to check odd or even using recursion
In this tutorial, we will discuss a concept of the C program to check odd or even from the given number using recursion
In this article, we are going to learn how to check odd and even numbers using recursion in the C programming language
Program 1
// C program to check odd or even using recursion #include <stdio.h> //recursive function to check if a number is even int isEven(int num){ if(num==0) return 1; //base case: 0 is even else if (num==1) return 0; //base case: 1 is odd else return isEven(num-2); //recursive case: reduce the number by 2 } int main() { int num; //input a number from the user printf("Enter a number: "); scanf("%d",&num); //check if the number is even or odd using recursion if (isEven(num)) printf("%d is even\n",num); else printf("%d is odd\n",num); return 0; }
When the above code is executed, it produces the following results
Case 1
Enter a number: 53
53 is odd
Case 2
Enter a number: 100
100 is even
Explanation
- The isEven() function reduces the number by 2 in each recursive call
- The base case is when the number reaches 0 or 1. If it reaches 0, the number is even. if it reaches 1, the number is odd
- The recursion continues until it matches one of the base case
Similar post
C program to find a number is even or odd using the function
C program to separate Odd and Even numbers from an array
C program to Electricity bill calculation using the function
C program to display all even and odd numbers from 1 to n
C program display odd and even numbers without if statements
C program to calculate the sum of odd and even numbers
C program to find whether a number is even or odd
C++ program to find a number is even or odd using the function
C++ program to separate Odd and Even numbers from an array
C++ program to display all even and odd numbers from 1 to n
C++ program calculate Average of odd and even numbers in an array
C++ program to calculate the sum of odd and even numbers
C++ program to find whether a number is even or odd
Java program to find a number is even or odd using the method
Java program to separate Odd and Even numbers from an array
Java program to display all even and odd numbers from 1 to n
Java program display odd and even numbers without if statements
Java program to calculate the sum of odd and even numbers
Java program to find whether a number is even or odd
Suggested for you