w3resource

C++ Exercises: Find the Sum of GP series

C++ For Loop: Exercise-32 with Solution

Write a program in C++ to find the Sum of GP series.

Pictorial Presentation:

C++ Exercises: Find the Sum of GP series

Sample Solution:-

C++ Code :

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    float g1,cr,i,n,j;
    int ntrm,gpn;
    float sum=0;
    cout << "\n\n Find the Sum of GP series:\n";
    cout << "-------------------------------\n";
    cout << " Input  the starting number of the G.P. series: ";
    cin >> g1;
    cout << " Input the number of items for  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<<g1<<"  ";
     sum=g1;
     for(j=1;j<ntrm;j++)
       {
        gpn=g1*pow(cr,j);
        sum=sum+gpn;
        cout<<gpn<<"  ";
       }  
/*-------- End of G.P. series generate ---------------*/
    cout<<"\n The Sum of the G.P. series:  "<<sum<<endl;	
}

Sample Output:

 Find the Sum of GP series:                                            
-------------------------------                                        
 Input  the starting number of the G.P. series: 3                      
 Input the number of items for  the G.P. series: 5                     
 Input the common ratio of G.P. series: 2                              
                                                                       
The numbers for the G.P. series:                                       
 3  6  12  24  48                                                      
 The Sum of the G.P. series:  93  

Flowchart:

Flowchart: Find the Sum of GP series

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to find out the sum of an A.P. series.
Next: Write a program in C++ to Check Whether a Number can be Express as Sum of Two Prime Numbers.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.

C++ Programming: Tips of the Day

What is the usefulness of `enable_shared_from_this?

It enables you to get a valid shared_ptr instance to this, when all you have is this. Without it, you would have no way of getting a shared_ptr to this, unless you already had one as a member.

class Y: public enable_shared_from_this
{
public:

    shared_ptr f()
    {
        return shared_from_this();
    }
}

int main()
{
    shared_ptr p(new Y);
    shared_ptr q = p->f();
    assert(p == q);
    assert(!(p < q || q < p)); // p and q must share ownership
}

The method f() returns a valid shared_ptr, even though it had no member instance. Note that you cannot simply do this:

class Y: public enable_shared_from_this
{
public:

    shared_ptr f()
    {
        return shared_ptr(this);
    }
}

The shared pointer that this returned will have a different reference count from the "proper" one, and one of them will end up losing and holding a dangling reference when the object is deleted.

Ref : https://bit.ly/3pwVzzz