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

Function in C language

Recursion in C language

If statements in C language

C programming operator

C code to Calculate average of odd and even in an array
C++ program to check odd or even using recursion
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago