Give a recursive algorithm to compute the sum of the cubes o

Give a recursive algorithm to compute the sum of the cubes of the first n positive integers. The input to the algorithm is a positive integer n. The output is sigma_j = 1^n j^3. The algorithm should be recursive, it should not compute the sum using a closed form expression or a loop.

Solution

// C++ code to recursively calculate sum of cubes of first n positive integers

#include <iostream>

using namespace std;

int sumCubes(int n)
{
// base case
if(n == 0)
return 0;
else
// make a recursive call
return (sumCubes(n-1) + n*n*n);
}
int main()
{
int n;
cout << \"Enter positive intege N: \";
cin >>n;
cout << \"Sum of cubes for first \" << n << \" positive integers: \" << sumCubes(n) << endl;
}


/*
output:

Enter positive intege N: 3
Sum of cubes for first 3 positive integers: 36


Enter positive intege N: 5
Sum of cubes for first 5 positive integers: 225

*/

 Give a recursive algorithm to compute the sum of the cubes of the first n positive integers. The input to the algorithm is a positive integer n. The output is

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site