← All resources

Write a program to pass an array to a function and return the position of the largest value using recursive technique

<h3>OUTPUT</h3>
Enter the size of the array:
5
Enter the values for the array:
21
12
23
11
15
The largest value is: 23
import java.util.*;
public class Largest_Recur
{
    int largest(int a[], int i, int maxp)
    {
        if(i==a.length)
        return maxp;
        if(a[i]>a[maxp])
        maxp=i;
        return largest(a, i+1, maxp);
    }
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        int arr[], n, i, pos;
        System.out.println("Enter the size of the array:");
        n = sc.nextInt();
        arr = new int[n];
        System.out.println("Enter the values for the array:");
        for(i=0;i < n;i++)
        {
            arr[i] = sc.nextInt();
        }
        Largest_Recur obj = new Largest_Recur();
        pos = obj.largest(arr, 0, 0);
        System.out.println("The largest value is: "+arr[pos]);
    }
}