Question

C++ Write a recursive function named sumSquares that returns the sum of the squares of the...

C++

Write a recursive function named sumSquares that returns the sum of the squares of the numbers from  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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int sumSquares(int i, int num) {
    if(i > num) {
        return 0;
    } else {
        return (i*i) + sumSquares(i+1, num);
    }
}

int main() {
    int num;
    cout << "Enter a value for num: ";
    cin >> num;
    cout << "Sum of squares of all numbers from 1 to " << num << " is " << sumSquares(1, num) << endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
C++ Write a recursive function named sumSquares that returns the sum of the squares of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT