Print Array Using Recursion inJava
Print Array | Recursion
public class reverseRec {
public static void printArr(int arr[], int i) {
if (i == arr.length) {
return;
}
System.out.println(arr[i]);
printArr(arr, i + 1);
}
public static void main(String[] args) {
int arr[] = { 1, 2, 3, 4, 5 };
printArr(arr, 0);
}
}
output:
1
2
3
4
5
Reverse Array | Recursion
public class reverseRec {
// print Reverse Array
public static void revPrintArr(int arr[], int i) {
if (i == arr.length) {
return;
}
revPrintArr(arr, i + 1);
System.out.println(arr[i]);
}
public static void main(String[] args) {
int arr[] = { 1, 2, 3, 4, 5 };
revPrintArr(arr , 0);
}
}
output:
5
4
3
2
1
No comments: