INTERNET

# How to Find the Sum of a Geometric Series Using Multiple Languages When looking to enhance your programming skills, you'll probably want to learn about geometric sequences at some point. In a geometric sequence, each term is found by multiplying the previous term by a constant.

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:

``a, ar, ar², ar³, ...``

where,

``a = First termr = Common ratio``

## 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:

``// C++ program to find the sum of geometric series#include <iostream>using namespace std;// Function to find the sum of geometric seriesfloat sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms){ float result = 0; for (int i=0; i<noOfTerms; i++) { result = result + firstTerm; firstTerm = firstTerm * commonRatio; } return result;}int main(){ float firstTerm = 1; float commonRatio = 2; int noOfTerms = 8; cout << "First Term: " << firstTerm << endl; cout << "Common Ratio: " << commonRatio << endl; cout << "Number of Terms: " << noOfTerms << endl; cout << "Sum of the geometric series: " << sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms) << endl;return 0;}``

Output:

``First Term: 1Common Ratio: 2Number of Terms: 8Sum of the geometric series: 255``

### 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:

``# Python program to find the sum of geometric series# Function to find the sum of geometric seriesdef sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms):    result = 0    for i in range(noOfTerms):        result = result + firstTerm        firstTerm = firstTerm * commonRatio    return resultfirstTerm = 1commonRatio = 2noOfTerms = 8print("First Term:", firstTerm)print("Common Ratio:", commonRatio)print("Number of Terms:", noOfTerms)print("Sum of the geometric series:", sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms))``

Output:

``First Term: 1Common Ratio: 2Number of Terms: 8Sum of the geometric series: 255``

### 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:

``// JavaScript program to find the sum of geometric series// Function to find the sum of geometric seriesfunction sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms) { var result = 0; for (let i=0; i<noOfTerms; i++) { result = result + firstTerm; firstTerm = firstTerm * commonRatio; } return result;}var firstTerm = 1;var commonRatio = 2;var noOfTerms = 8;document.write("First Term: " + firstTerm + "<br>");document.write("Common Ratio: " + commonRatio + "<br>");document.write("Number of Terms: " + noOfTerms + "<br>");document.write("Sum of the geometric series: " + sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));``

Output:

``First Term: 1Common Ratio: 2Number of Terms: 8Sum of the geometric series: 255``

### 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:

``// C program to find the sum of geometric series#include <stdio.h>// Function to find the sum of geometric seriesfloat sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms){ float result = 0; for (int i=0; i<noOfTerms; i++) { result = result + firstTerm; firstTerm = firstTerm * commonRatio; } return result;}int main(){ float firstTerm = 1; float commonRatio = 2; int noOfTerms = 8; printf("First Term: %f \⁠n", firstTerm); printf("Common Ratio: %f \⁠n", commonRatio); printf("Number of Terms: %d \⁠n", noOfTerms); printf("Sum of the geometric series: %f \⁠n", sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));return 0;}``

Output:

``First Term: 1Common Ratio: 2Number of Terms: 8Sum of the geometric series: 255``

## 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:

``Sum of geometric series = a(1 – rn)/(1 – r)``

where,

``a = First termd = Common ration = No. of terms``

### 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:

``// C++ program to find the sum of geometric series#include <bits/stdc++.h>using namespace std;// Function to find the sum of geometric seriesfloat sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms){ return (firstTerm * (1 - pow(commonRatio, noOfTerms))) / (1 - commonRatio);}int main(){ float firstTerm = 1; float commonRatio = 2; int noOfTerms = 8; cout << "First Term: " << firstTerm << endl; cout << "Common Ratio: " << commonRatio << endl; cout << "Number of Terms: " << noOfTerms << endl; cout << "Sum of the geometric series: " << sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms) << endl;return 0;}``

Output:

``First Term: 1Common Ratio: 2Number of Terms: 8Sum of the geometric series: 255``

### 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:

``# Python program to find the sum of geometric series# Function to find the sum of geometric seriesdef sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms):    return (firstTerm * (1 - pow(commonRatio, noOfTerms))) / (1 - commonRatio)firstTerm = 1commonRatio = 2noOfTerms = 8print("First Term:", firstTerm)print("Common Ratio:", commonRatio)print("Number of Terms:", noOfTerms)print("Sum of the geometric series:", sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms))``

Output:

``First Term: 1Common Ratio: 2Number of Terms: 8Sum of the geometric series: 255``

### 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:

``// JavaScript program to find the sum of geometric series// Function to find the sum of geometric seriesfunction sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms) { return (firstTerm * (1 - Math.pow(commonRatio, noOfTerms))) / (1 - commonRatio);}var firstTerm = 1;var commonRatio = 2;var noOfTerms = 8;document.write("First Term: " + firstTerm + "<br>");document.write("Common Ratio: " + commonRatio + "<br>");document.write("Number of Terms: " + noOfTerms + "<br>");document.write("Sum of the geometric series: " + sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));``

Output:

``First Term: 1Common Ratio: 2Number of Terms: 8Sum of the geometric series: 255``

### 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:

``// C program to find the sum of geometric series#include <stdio.h>#include <math.h>// Function to find the sum of geometric seriesfloat sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms){ return (firstTerm * (1 - pow(commonRatio, noOfTerms))) / (1 - commonRatio);}int main(){ float firstTerm = 1; float commonRatio = 2; int noOfTerms = 8; printf("First Term: %f \⁠n", firstTerm); printf("Common Ratio: %f \⁠n", commonRatio); printf("Number of Terms: %d \⁠n", noOfTerms); printf("Sum of the geometric series: %f \⁠n", sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));return 0;}``

Output:

``First Term: 1Common Ratio: 2Number of Terms: 8Sum of the geometric series: 255``

## 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. Previous article

Next article

INTERNET

INTERNET

INTERNET

INTERNET

INTERNET

INTERNET