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
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;
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);
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.
If you wish to look at all C++ Programming examples, go to C++ Programs
- 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,
- Chapterwise Mock Tests: Chapter 1,
, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry
. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms.
Stay connected with him at LinkedIn
Subscribe to his free Masterclasses at Youtube
& technical discussions at Telegram SanfoundryClasses