Jump to content

How to Find the Sum of a Geometric Series Using Multiple Languages - Other Helpful Tutorials - InviteHawk - The #1 Trusted Source for Free Tracker Invites

Buy, Sell, Trade, or Find Free Invites for top private trackers like redacted, blutopia, losslessclub, femdomcult, filelist, Chdbits, Uhdbits, empornium, iptorrents, hdbits, gazellegames, animebytes, privatehd, myspleen, torrentleech, morethantv, bibliotik, alpharatio, blady, passthepopcorn, brokenstones, pornbay, cgpeers, cinemageddon, broadcasthenet, learnbits, torrentseeds, beyondhd, cinemaz, u2.dmhy, Karagarga, PTerclub, Nyaa.si, Polishtracker, and many more.

Recommended Posts


  • Member ID:  53,629
  • Followers:  0
  • Topic Count:  3,075
  • Topics Per Day:  1.55
  • Content Count:  3,094
  • Content Per Day:  1.56
  • Reputation:   105
  • Achievement Points:  4,020
  • Days Won:  0
  • Joined:  01/24/2021
  • Status:  Offline
  • Last Seen:  

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 term r = 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 series float 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: 1 Common Ratio: 2 Number of Terms: 8 Sum 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 series def sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms): result = 0 for i in range(noOfTerms): result = result + firstTerm firstTerm = firstTerm * commonRatio return result firstTerm = 1 commonRatio = 2 noOfTerms = 8 print("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: 1 Common Ratio: 2 Number of Terms: 8 Sum of the geometric series: 255

RELATED:How To Print "Hello, World!" In The Most Popular Programming Languages

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 series function 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: 1 Common Ratio: 2 Number of Terms: 8 Sum 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 series float 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: 1 Common Ratio: 2 Number of Terms: 8 Sum 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 term d = Common ratio n = 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 series float 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: 1 Common Ratio: 2 Number of Terms: 8 Sum 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 series def sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms): return (firstTerm * (1 - pow(commonRatio, noOfTerms))) / (1 - commonRatio) firstTerm = 1 commonRatio = 2 noOfTerms = 8 print("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: 1 Common Ratio: 2 Number of Terms: 8 Sum of the geometric series: 255

RELATED:How To Find The LCM And GCD Of Two Numbers In Multiple Languages

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 series function 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: 1 Common Ratio: 2 Number of Terms: 8 Sum of the geometric series: 255

RELATED:How To Count The Occurrences Of A Given Character In A String

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 series float 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: 1 Common Ratio: 2 Number of Terms: 8 Sum 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.


  • Member ID:  48,211
  • Followers:  60
  • Topic Count:  144
  • Topics Per Day:  0.06
  • Content Count:  30,043
  • Content Per Day:  13.19
  • Reputation:   2,205
  • Achievement Points:  38,196
  • Days Won:  12
  • Joined:  04/09/2020
  • Status:  Offline
  • Last Seen:  

Avoid unnecessary posts such as 'Thank you', 'Welcome', etc. Such posts will be deleted and user will be warned if it happens again. If caught spamming, the following actions are applicable -

  • First time - Warning
  • Second time - 5000 Points will be deducted
  • Third time - Ban for 7 days
  • Fourth time - Permanent Ban

If the post helped you, reward the user by reacting to the post like this -

1.jpg

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Read this before posting -
  • Only post if you have something valuable to contribute.
  • Avoid unnecessary posts such as 'Thank you', 'Welcome', etc. Such posts will be deleted and you will be warned if it happens again.
  • If the post helped you, reward the user by reacting to the post like this -                      1.jpg
Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Customer Reviews

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.