Answer to Question #173441 in C++ for JOTHIBASS S

Question #173441

Develop a C++ program to calculate sum of square root from 1 to n numbers using recursive functions


1
Expert's answer
2021-03-21T04:46:44-0400
#include <cmath>
#include <iostream>

using namespace std;

double sum_sqrt(int n)
{
    double res = sqrt((double)n);
    if (n > 1)
    {
        res += sum_sqrt(n - 1);
    }
    return res;
}

int main()
{
    int num;
    cout << "Enter a number: ";
    cin >> num;
    if (num >= 0)
    {
        cout << "Sum of square roots from 1 to " << num << " is " << sum_sqrt(num);
    }
    else
    {
        cout << "Cannot calculate the square root of negative number.";
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS