Given an integer n > 0, write a recursive C++ function that returns the sum of the squares of 1 through n .
Expert Answer
Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: Given an integer n > 0, write a recursive C++ function that returns the sum of the…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Program :
#include<iostream>
using namespace std;
int sumSquares(int n);
int main()
{
int n;
cout<<“Please enter a number : “;
cin>>n;
while(n<0)
{
cout<<“nPlease enter a number greater than 1 : “;
cin>>n;
}
cout<<“The sum of squares of “<<n<<” is “<<sumSquares(n);
return 0;
}
int sumSquares(int n)
{
if (n == 0)
return (0);
else
{
return (sumSquares(n-1) + (n*n));
}
}
Output :