C++ Program to Find the Sum of Geometric Progression Series

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.

  1. /*
  2.  * C++ Program to Calculate Sum of Geometric Progression
  3.  */
  4. #include <iostream>
  5.  
  6. void calSumGP(float a, float r, int n)
  7. {
  8.     float sum = 0, temp = a;
  9.  
  10.     for (int i = 1; i < n; i++)
  11.     {
  12.         sum = sum + temp;
  13.         temp = temp * r;
  14.     }
  15.     std::cout << "Sum of geometric progression : "
  16.               << sum << std::endl;
  17. }
  18.  
  19. int main()
  20. {
  21.     int n;
  22.     float a, r;
  23.  
  24.     std::cout << "Enter the value of a = ";
  25.     std::cin  >> a;
  26.     std::cout << "Enter the value of r = ";
  27.     std::cin  >> r;
  28.     std::cout << "Enter the value of n = ";
  29.     std::cin  >> n;
  30.     calSumGP(a, r, n);
  31. }

$ 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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, 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 & discussions at Telegram SanfoundryClasses.