write the program to find the sum of first 20 terms of series: S=2/3+4/5+8/7+16/9+............
Answers
float sum=0.0;
for(int i=0;i<=20;i++){
float k=math.pow(2,i) / (2*i+1);
sum+=k;
System.out.print(sum);
}
this is java program in c++ as follows
#include<math.h>
float sum=0.0;
for(int i=0;i<=20;i++){
float k=pow(2,i) / (2i+1);
sum+=k;
cout<<sum;
}
Program code to find the sum of 1st 20 terms of the series is:
#include <bits/stdc++.h>
using namespace std;
// Function to find sum of series upto n terms where n=20
//double datatype used to store the sum value
double Sum(int n)
{
// initializing counter by 1
int i = 1;
// variable to calculate result
double result = 0.0;
bool sign = true;
// while loop until nth term is not reached
while (n > 0)
{ n--;
// bool type variable for checking and validating is used
if (sign)
{ sign = !sign;
result = result + (double)++i / ++i;
}
else
{ sign = !sign;
result = result - (double)++i / ++i;
}
}
return result;
}
int main()
{ int n = 20;
cout << Sum(n);
return 0;
}