This C++ program calculates the sum of geometric progression. The program takes the first term, the common ratio and the number of terms as input. A function is called to calculate the sum of the progression and to print it.
Here is the source code of the C++ program calculates the sum of geometric progression. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Calculate Sum of Geometric Progression
*/
#include <iostream>
void calSumGP(float a, float r, int n)
{
float sum = 0, temp = a;
for (int i = 1; i < n; i++)
{
sum = sum + temp;
temp = temp * r;
}
std::cout << "Sum of geometric progression : "
<< sum << std::endl;
}
int main()
{
int n;
float a, r;
std::cout << "Enter the value of a = ";
std::cin >> a;
std::cout << "Enter the value of r = ";
std::cin >> r;
std::cout << "Enter the value of n = ";
std::cin >> n;
calSumGP(a, r, n);
}
$ a.out Enter the value of a = 1 Enter the value of r = 2 Enter the value of n = 10 Sum of geometric progression : 511
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Programming MCQs
- Practice Computer Science MCQs
- Check Computer Science Books
- Apply for Computer Science Internship
- Check Programming Books