Last active
March 31, 2019 08:29
-
-
Save theArjun/b0579cc088dd62db1c182b3635ef8997 to your computer and use it in GitHub Desktop.
Reverse Array By Recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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