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.
#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;
#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) {
doubleconst term_i = a * std::pow(r, i);
std::cout << "term " << i << " = " << term_i << '\n';
sum += term_i;
}
std::cout << sum << '\n';
}