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.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy C++ Books
- Buy Computer Science Books
- Apply for C++ Internship
- Apply for Computer Science Internship
- Apply for Information Technology Internship