155,775 views
25 votes
25 votes
Write a recursive function named canmakesum that takes a list of * integers and an integer target value (sum) and returns true if it is * possible to have some set of values from the list that sum to the * target value.

User Andreas Bigger
by
2.7k points

1 Answer

10 votes
10 votes

// Java program to find sum of array

// elements using recursion.

class Test {

static int arr[] = { 1, 2, 3, 4, 5 };

// Return sum of elements in A[0..N-1]

// using recursion.

static int findSum(int A[], int N)

{

if (N <= 0)

return 0;

return (findSum(A, N - 1) + A[N - 1]);

}

// Driver method

public static void main(String[] args)

{

System.out.println(findSum(arr, arr.length));

}

}

User Lbatson
by
3.0k points