Wednesday 18 June 2014

C++ program to find nth term Geometric Progression(GP) series and its sum



Here is a C++ program to find nth term Geometric Progression(GP) series and its sum


 SOURCE CODE OUTPUT
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    float r,n,a1,an,sum;
    cout<<"Enter first term ";
    cin>>a1;
    cout<<"Enter common ratio ";
    cin>>r;
    cout<<"Enter the value of n ";
    cin>>n;
    an=a1*pow(r,n-1);
    if(r>1)
        sum=a1*(pow(r,n)-1)/(r-1);
    else
        sum=a1*(1-pow(r,n))/(1-r);
    cout<<n<<"th term of G.P. is "<<an;
    cout<<endl<<"Sum : "<<sum<<endl;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment