Write a C++ program to calculate the sum of finite GP series and display the result on screen.
Program Code:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float x,r,i,m;
int n;
float sum=0;
cout<<"Enter the first number of the GP series : ";
cin>>x;
cout<<endl;
cout<<"Enter the number of terms in the GP series : ";
cin>>n;
cout<<endl;
cout<<"Enter the common ratio of the GP series : ";
cin>>r;
cout<<endl;
sum = (x * ( 1 - pow( r, n+1 ) ) ) / (1 - r);
m = x * pow ( r, n-1 );
cout<<"Sum of all the numbers of the GP series is : ";
cout<<sum;
return 0;
}
The program output is tested on www.jdoodle.com
Output:
Enter the first number of the GP series : 1
Enter the number of terms in the GP series : 10
Enter the common ratio of the GP series : 2
Sum of all the numbers of the GP series is : 2047
Thanks
Mukesh Rajput
Post A Comment:
0 comments: