Answer to Question #173886 in C++ for sivasurya

Question #173886

 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:47:15-0400

#include <cmath>

#include <iostream>

 

using namespace std;

double sum_sqrt(int n);

 

int main() {

   int n;

   cout << "Enter N: ";

   cin >> n;

   

   if (n >= 0)    

       cout << "Sum of square root from 1 to " << n

                << " numbers is " << sum_sqrt(n)<<endl;

   else   

       cout << "N must be positive. Cannot calculate."<<endl;

   return 0;

}

 

double sum_sqrt(int n) {

   double sum;

   sum = sqrt((double)n);

   if (n > 1) {

       sum += sum_sqrt(n - 1);

   }

   return sum;

}

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