# Java Program to Find Odd Numbers in an Array by Using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Fins Sum of Digits of a Number 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 Odd Numbers in an Array by Using Recursion

Lets assume there is an array say A which has 5 elements {77, 82, 100, 17, 95}

Odd numbers in the array A = 77, 17, 95

Total odd numbers in the array A = 3

Now let’s see different ways to find odd numbers in an Array by using Recursion.

### Method-1: Java Program to Find Odd Numbers in an Array By Using Static Input and Recursion

Approach:

• Declare and initiate an integer Array ‘A[]‘.
• Call a user defined method countOdds() and pass the array ‘A[]’ with first index and last 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 odd then increment count value and call the countOdds() method recursively by passing the array ‘A[]’ with next index and last index of the array as parameter.
• Now the value of the user defined method countOdds() is stored in an integer variable say ‘m’.
• Print the number of odd 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[] = {77, 82, 100, 17, 95};
//calling the user defined method countodds()
//and store the result value inside an integer variable say ‘m’
int m = countOdds(A, 0, A.length - 1);
//print the result
System.out.println("The number of odd elements in the array are: " + m);
}

//countOdds() method
public static int countOdds(int A[], int firstIndex, int lastIndex)
{
int count = 0;
//checking numbers of elements in the array
if(firstIndex <= lastIndex)
{
// if the element is odd then the count is incremented
if(A[firstIndex] % 2 != 0)
{
count++;
}
count+=countOdds(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 countOdds() and pass the array ‘A[]’ with first index and last 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 odd then increment count value and call the countOdds() method recursively by passing the array ‘A[]’ with next index and last index of the array as parameter.
• Now the value of the user defined method countOdds() is stored in an integer variable say ‘m’.
• Print the number of odd 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 countOdds()
//and store the result value inside an integer variable say ‘m’
int m = countOdds(A, 0, A.length - 1);
//print the result
System.out.println("The number of odd elements in the array are: " + m);
}

//countOdds() method
public static int countOdds(int A[], int firstIndex, int lastIndex)
{
int count = 0;
//checking numbers of elements in the array
if(firstIndex <= lastIndex)
{
// if the element is odd then the count is incremented
if(A[firstIndex] % 2 != 0)
{
count++;
}
count+=countOdds(A, firstIndex + 1, lastIndex);
}
return count;
}
}

Output:

Enter no. of elements you want in array:
5
Enter all the elements:
11
22
33
44
55
The number of odd 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: