Question
output formatting option 1: for n=5 1+1/(1+2)+1/(2+3)+1/(3+4)+1/(4+5)=1.7873 where 1+1/(1+2)+1/(2+3)+1/(3+4)+1/(4+5), =, and 1.7873 to be produced and printed by the recursive function; 1/(0+1) to be printed
output formatting
option 1: for n=5 1+1/(1+2)+1/(2+3)+1/(3+4)+1/(4+5)=1.7873 where "1+1/(1+2)+1/(2+3)+1/(3+4)+1/(4+5)", "=", and 1.7873 to be produced and printed by the recursive function; 1/(0+1) to be printed as 1
option 2: for n=3 (3*3)+(2*2) +1 =14 where "(3*3)+(2*2) +1 =14", "=" and "14" to be produced and printed by the recursive function; (1*1) to be printed as 1
how can I fix my output formatting
Code:
#include
// recursion function for the series: 1/(0+1)+1/ (1+2)+1/(2+3)+1/(3+4)+...+1/((n-1)+n)- starting with 1 float series1(int n) { if (n == 1) { return 1.0; } return float(1 / float(n + n - 1)) + series1(n - 1); // recursive case }
// recursion function for the series: (n*n)+((n-1) *(n-1)+...+(3*3)+(2*2)+(1*1) [ starting with n] int series2(int n) { if (n == 1) { return 1; } return n * n + series2(n - 1); // recursive case } int main() { int choice, num; while (choice!=3) { // while loop until user exit cout << " Enter choice: "" (1) for sum of series: 1/(0+1)+1/ (1+2)+1/(2+3)+1/(3+4)+...+1/((n-1)+n)- starting with 1 " " (2) for sum of series: (n*n)+((n-1) *(n-1)+...+(3*3)+(2*2)+(1*1) [ starting with n] " " (3) To exit " " Enter your choice: "; cin >> choice;
if (choice == 1 || choice == 2) { cout << " Enter the value of n: "; //get input cin >> num; } // switch case switch (choice) { case 1: cout << setprecision(6) << fixed << " Sum of the terms of series 1 is: " << series1(num) << " "; break; case 2: cout << " Sum of the terms of series 2 is: " << series2(num) << endl; break; case 3: cout<< " goodbye"; exit(0); // to exit from the while loop default: cout << " Enter a valid choice" << endl; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started