#include <iostream>
using namespace std;
void main()
{
float g1,cr,i,n,j;
int ntrm,gpn;
float sum=0;
cout<<"Find the Sum of GP series.:\n ";
cout<<"Input the first number of the G.P. series: ";
cin>>g1;
cout<<"Input the number or terms in the G.P. series: ";
cin>>ntrm;
cout<<"Input the common ratio of G.P. series: ";
cin>>cr;
/*-------- generate G.P. series ---------------*/
cout<<"\nThe numbers for the G.P. series:\n ";
cout<<"1 ";
for(j=1;j<=ntrm;j++)
{
gpn=pow(cr,j);
cout<<" "<<gpn;
}
/*-------- End of G.P. series generate ---------------*/
sum = (g1*(1 - (pow(cr,ntrm+1))))/(1-cr);
n = g1 * (pow(cr,ntrm-1));
cout<<"\nThe tn terms of G.P. : "<<n;
cout<<"\nThe Sum of the G.P. series : "<<sum<<endl;
system ("pause");
}
Test Data :
Input the first number of the G.P. series: 1
Input the number or terms in the G.P. series: 5
Input the common ratio of G.P. series: 2
Expected Output :
The numbers for the G.P. series:
1 2 4 8 16 32
The n terms of G.P. : 16.000000
The Sum of the G.P. series : 63.000000
Write a program in c++ to find the Sum of GP series
Reviewed by Unknown
on
June 20, 2017
Rating:
No comments: