Skip to content

Instantly share code, notes, and snippets.

@theArjun
Last active March 31, 2019 08:29
Show Gist options
  • Select an option

  • Save theArjun/b0579cc088dd62db1c182b3635ef8997 to your computer and use it in GitHub Desktop.

Select an option

Save theArjun/b0579cc088dd62db1c182b3635ef8997 to your computer and use it in GitHub Desktop.
Reverse Array By Recursion
import java.util.Scanner;
class ReverseArray {
public static void reverse(int array[], int start, int end) {
if(start >= end) {
return;
}
int temp = array[end];
array[end] = array[start];
array[start] = temp;
reverse(array, start+1, end-1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// No of array elements
int n = sc.nextInt();
int[] array = new int[n];
// Prompting user for array elements
for(int i = 0; i< n; i++) {
array[i] = sc.nextInt();
}
// Function Call
reverse(array,0,n-1);
// Printing reversed array
for(int i = 0; i<n;i++){
System.out.print(array[i] + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment