Prerequisite: Recursion in Java
In the previous article, we have discussed about Java Program to Find Odd Numbers in an Array by Using Recursion
In this program we are going to see how to find odd numbers in an Array by using Recursion in Java programming language.
Java Program to Find Even Numbers in an Array by Using Recursion
Lets assume there is an array say A[] which has 5 elements {70, 82, 33, 17, 95}
Even numbers in the array A = 70, 82
Total even numbers in the array A = 2
Now let’s see different ways to find odd numbers in an Array by using Recursion.
Method-1: Java Program to Find Even Numbers in an Array By Using Static Input and Recursion
Approach:
- Declare and initiate an integer Array ‘
A[]
‘. - Call a user defined method
countEven()
and pass the array ‘A[]
’ withfirst index
andlast index
of the array as parameter. - Inside the user defined method we will declare and initialize an integer variable say ‘
count
’ as 0. - Check the element is even or odd if the element is even then increment count value and call the
countEven()
method recursively by passing the array ‘A[]
’ withnext index
andlast index
of the array as parameter. - Now the value of the user defined method
countEven()
is stored in an integer variable say ‘m
’. - Print the number of even elements in the array.
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { //declare and initialize an array A int A[] = {70, 82, 33, 17, 95}; //calling the user defined method countEven() //and store the result value inside an integer variable say ‘m’ int m = countEven(A, 0, A.length - 1); //print the result System.out.println("The number of even elements in the array are: " + m); } //countEven() method public static int countEven(int A[], int firstIndex, int lastIndex) { int count = 0; //checking numbers of elements in the array if(firstIndex <= lastIndex) { // if the element is even then the count is incremented if(A[firstIndex] % 2 == 0) { count++; } count+=countEven(A, firstIndex + 1, lastIndex); } return count; } }
Output: The number of odd elements in the array are: 3
Method-2: Java Program to Find Odd Numbers in an Array by Using Recursion By Using User Input and Recursion
Approach:
- Create a scanner class.
- Declare an integer variable say ‘
n
’ - Prompt the user to enter the size of the array.
- Declare an integer array say ‘
A[]
’ - Prompt the user to enter the elements inside the array.
- The elements inside the Array A[] are added in sequential index position using a for loop. Where the loop starts from i=0 to i<n and the loop is incremented by 1.
- Call a user defined method
countEven()
and pass the array ‘A[]
’ withfirst index
andlast index
of the array as parameter. - Inside the user defined method we will declare and initialize an integer variable say ‘
count
’ as 0. - Check the element is even or odd if the element is even then increment count value and call the
countEven()
method recursively by passing the array ‘A[]
’ withnext index
andlast index
of the array as parameter. - Now the value of the user defined method
countEven()
is stored in an integer variable say ‘m
’. - Print the number of even elements in the array.
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { // create a scanner class Scanner s = new Scanner(System.in); System.out.println("Enter no. of elements you want in array:"); //declare the size of the array int n = s.nextInt(); // declare the array and initialize it with user input int A[] = new int[n]; System.out.println("Enter all the elements:"); //get all the elements sequentially for(int i = 0; i < n; i++) A[i] = s.nextInt(); //calling the user defined method CountEven() //and store the result value inside an integer variable say ‘m’ int m = countEven(A, 0, A.length - 1); //print the result System.out.println("The number of even elements in the array are: " + m); } //countEven() method public static int countEven(int A[], int firstIndex, int lastIndex) { int count = 0; //checking numbers of elements in the array if(firstIndex <= lastIndex) { // if the element is even then the count is incremented if(A[firstIndex] % 2 == 0) { count++; } count+=countEven(A, firstIndex + 1, lastIndex); } return count; } }
Output: Enter no. of elements you want in array: 5 Enter all the elements: 2 3 4 5 6 The number of even elements in the array are: 3
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Related Java Programs: