im stuck on this

A geometric series is defined by the following:

a + ar + ar2 + ar3 + ˙˙˙ + arn-1

a is the first term.
r is the “common ratio.”
n is the number of terms in the series

Using this information, write a C++ program that uses a loop to display each term and determine the sum of a geometric series. Your program should prompt the user to enter values for a, r, and n. Test your program for a = 1, r = .5, and n = 10. Make sure your program displays the value it has calculated.
Start by getting the values for a, r, n, from the user.
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int a(0), r(0), n(0);

cout << "Enter values for a, r, and n: ";
cin >> a, r, n;
cout << endl;
cout << "You entered:" << endl;
cout << "a = " << a << " r = " << r << " n = " << n << endl;
cout << endl;



system("pause");
return 0;
}
im just stuck on the loop part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cmath>

int main()
{
    double a, r = 0., n = 0.;
    std::cin >> a >> r >> n;

    std::cout << "a = " << a << ", r = " << r << ", n = " << n << '\n';

    double sum = 0.; 
    for (int i = 0; i < n; ++i) { 
      double const term_i = a * std::pow(r, i); 
      std::cout << "term " << i << " = " << term_i << '\n';
      sum += term_i;
    }
    std::cout << sum << '\n';
}


Live demo:
http://coliru.stacked-crooked.com/a/58874dd8e7080f5f
Last edited on
Topic archived. No new replies allowed.