Write a program using iterative loop to calculate (numerically) the series:
1/x + 1/x 2 + 1/x3 + 1/x4 + . . . + 1/xn
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
cout<<"\nEnter n: ";
cin>>n;
int x;
cout<<"\nEnter x: ";
cin>>x;
float sum=0;
for(int i=1;i<=n;i++){
sum+=(1.0/float (pow(x,i)));
}
cout<<"\nSum = "<<sum;
return 0;
}
Comments
Leave a comment