Codeforcoding

Java program to check odd or even using recursion

Java program to check odd or even using recursion

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

 

How do we identify whether a given number is odd or even?

when you divide a number by two and if the balance is zero, it is an even number

when you  divided a number by two  and if the balance is one, it is an odd number

Java program to check odd or even using recursion
Display even and odd number using recursion

Example of even number 2,4,6,8,…..

Example of odd number 1,3,5,7,…..

 

Here we will use a modular operator to display odd or even number in the given range.

if n%2==0,  n is a even number

if n%2==1,  n is a odd number

Here’s a Java program to check whether given number is odd or even using recursion

Program 1

// Java code to check the given number odd or even
import java.util.Scanner;
class OddEvenCheck{

//recursive function to check if a number is even
public static Boolean isEven(int n){
//Base cases
if(n==0){
return true;//0 is even

}else if(n==1){
return false;//1 is odd

}else{
return isEven(n-2);
}


}
public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
 System.out.println("Enter a number to check odd or even:");
int num=input.nextInt();

//Check if the number is even
if(isEven(num)){

System.out.println(num+ " is even");

}else{
System.out.println(num+ " is odd");
}
input.close();
}
}

When the above code is executed, it produces the following result

Case 1

Enter a number to check odd or even:
45
45 is odd

 

 

Case 2

Enter a number to check odd or even:
90
90 is even

 

Explanation

The recursive function isEven(int n) checks whether the number n is even or odd recursively by subtracting 2 from the number until it reaches either 0 (even) or 1 (odd)

 

In the main method , the user is prompt to input a number , and based on the result from isEven(), the program gives the output whether are number is odd or even

 

This program works continually reducing the number by 2 and using the fact that even numbers reach 0, while odd numbers reach 1

 

Suggested for you

for loop in C++ language

While loop in C++ language

Do-while loop in C++ language

if statements in C++ language

 

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

 

Python program check whether a number is odd or even

Python program to check a number is even or odd using the function

Python program to display even and odd numbers without if

Python program to display even and odd number in the given range

Separate odd and even numbers in a list to different two list

Python Program to find odd and even numbers from a list

 

Java program to calculate sum of odd and even numbers

C++ program to calculate sum of odd and even numbers

Python program to calculate sum of odd and even numbers

 

PHP program to check whether a number prime or not
Exit mobile version