Question: C++: Write a recursive function named sumSquares that returns the sum of the squares of the numbers from 0 to num, in which num is
C++: Write a recursive function named sumSquares that returns the sum of the squares of the numbers from 0 to num, in which num is a nonnegative int variable. Do not use global variables; use the appropriate parameters. Also, write a program to test your function.
So I wrote the code but I need help with the error handling on when the user enters zero and a program to test my function.
-------------------------------------------------
#include
#include
using namespace std;
// set the variable
int sumSquares(int num);
int main()
{
// variables for the while loops
string answer;
char yes, no;
// a while loop for possiblilty of more than one entry
while (true)
{
//prompt the user for a number
cout << "Please input a number to sum the squares from 1 to that number: " << endl;
// get that number
int num;
cin >> num;
cout << " Sum is " << sumSquares(num) << endl;
// ask the user if they want to type another number
cout << " Do you want enter another number (yes or no)" << endl;
cin >> answer;
// if no is entered the program exits or yes and the program goes back to the begining
if (answer == "no" || answer == "No")
break;
}// end while loop
}
int sumSquares(int num)
{
// for when the user enters 0
if (num == 0)
{
return 0;
}
// as long as the number is positive
else
{
return num * num + sumSquares(num - 1);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
