biswasrohit20

java recursion

Jun 2nd, 2021 (edited)
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.46 KB | None | 0 0
  1. // Java program to find sum of array elements using recursion.
  2.  
  3. class Test {
  4.     static int arr[] = { 1, 2, 3, 4, 5 };
  5.  
  6.     // Return sum of elements in A[0..L-1] using recursion.
  7.     static int findSum(int A[], int L)
  8.     {
  9.         if (L <= 0)
  10.             return 0;
  11.         return (findSum(A, L - 1) + A[L - 1]);
  12.     }
  13.  
  14.     // Driver method
  15.     public static void main(String[] args)
  16.     {
  17.         System.out.println(findSum(arr, arr.length));
  18.     }
  19. }
Add Comment
Please, Sign In to add comment