Google Add

Search

Java Program to Reverse an Array using Recursion

Write a java program to reverse an array using recursion. Given an input array, we have to write a java code to reverse an array using recursion. In this tutorial, We are going to solve this problem using recursion.

Recursion is an important programming concept, if you are not familiar with recursion concept then you can check this tutorial on recursion.

Difference between recursion and iteration

Recursion objective questions for practice

Java program to reverse an array using iteration


Java program to reverse an array using recursion



Java Program to Reverse an Array using Recursion


Let's write a java code to reverse an array using recursion.


import java.util.Scanner;

public class ReverseArrayRecursion {

  public static void reverse(int arr[], int start, int end) {

      int temp;

      //If start index is greater than end index
      if(start >= end)
        return;

      //Logic to swap values
      temp = arr[start];
      arr[start] = arr[end];
      arr[end] = temp;

      //Recursively call
      reverse(arr, start+1, end-1);
   }


  public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       System.out.println("Enter the size of an array");
       int n = in.nextInt();

       //Declare an array
       int arr[] =  new int[n];

       System.out.println("Enter an array values");
        
       //Input array values
       for(int i = 0; i < n; i++) {
          arr[i] = in.nextInt();
       }

       reverse(arr, 0, n-1);
       
       System.out.println("Reverse of an array is ");

       for(int i = 0; i < n; i++) {
          System.out.println(arr[i]);
       }
  }

}



Java program to reverse an array using recursion - Video Tutorial


No comments:

Post a Comment