In this article, you’ll learn how to find the sum of the geometric series using Python, C++, JavaScript, and C.

What Is a Geometric Series?

The sum of the terms of an infinite geometric sequence is called a geometric series. The geometric sequence or geometric progression is denoted as follows:

where,

Problem Statement

You’re given the first term, common ratio, and no. of terms of the geometric series. You need to find the sum of the geometric series. Example: Let firstTerm = 1, commonRatio = 2, and noOfTerms = 8. Geometric Series: 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 Sum of the geometric series: 255 Thus, the output is 255.

Iterative Approach to Find the Sum of a Geometric Series

First, let’s take a look at the iterative way to find a geometric series’ sum. You’ll find out how to do this with each main programming language below.

C++ Program to Find the Sum of a Geometric Series Using Iteration

Below is the C++ program to find the sum of a geometric series using iteration:

Output:

Python Program to Find the Sum of a Geometric Series Using Iteration

Below is the Python program to find the sum of a geometric series using iteration:

Output:

JavaScript Program to Find the Sum of a Geometric Series Using Iteration

Below is the JavaScript program to find the sum of a geometric series using iteration:

Output:

C Program to Find the Sum of a Geometric Series Using Iteration

Below is the C program to find the sum of a geometric series using iteration:

Output:

An Efficient Approach to Find the Sum of a Geometric Series Using Formula

You can use the following formula to find the sum of the geometric series:

where,

C++ Program to Find the Sum of a Geometric Series Using Formula

Below is the C++ program to find the sum of a geometric series using the formula:

Output:

Python Program to Find the Sum of a Geometric Series Using Formula

Below is the Python program to find the sum of a geometric series using the formula:

Output:

JavaScript Program to Find the Sum of a Geometric Series Using Formula

Below is the JavaScript program to find the sum of a geometric series using the formula:

Output:

C Program to Find the Sum of a Geometric Series Using Formula

Below is the C program to find the sum of a geometric series using the formula:

Output:

Now You Know How to Find Geometric Series Sums Using Different Programming Languages

In this article, you learned how to find the sum of geometric series using two approaches: iteration and formula. You also learned how to solve this problem using various programming languages like Python, C++, JavaScript, and C.

Python is a general-purpose programming language with a focus on code readability. You can use Python for data science, machine learning, web development, image processing, computer vision, etc. It’s one of the most versatile programming languages. It’s very much worth exploring this powerful programming language.