PHP Program to Find Sum of Geometric Series Last Updated : 27 Dec, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a number n, the task is to find the sum of the geometric series in PHP. A Geometric series is a series with a constant ratio between successive terms. The first term of the series is denoted by a and the common ratio is denoted by r. PHP Program to Sum of Geometric Series using for LoopA Simple solution to calculate the sum of geometric series. Here, we use a for loop to find the sum of the Geometric Series. Example: PHP Program to find the sum of geometric series using a for loop. PHP <?php function geometricSum($a, $r, $n) { $sum = 0; for ($i = 0; $i < $n; $i++){ $sum = $sum + $a; $a = $a * $r; } return $sum; } // Driver code $a = 2; // First term $r = 3; // Common ratio $n = 4; // Number of terms echo "Sum of Geometric Series: " . geometricSum($a, $r, $n); ?> OutputSum of Geometric Series: 80 PHP Program to Sum of Geometric Series using Mathematical FormulaThe series looks like this :- a, ar, ar2, ar3, ar4, . . . Sum of series Sn = a×(1−rn)1−rSn=1−ra×(1−rn)Where: Sn: The sum of the geometric series up to the nth term.a: The first term of the series.r: The common ratio of the series.n: The number of terms in the series.Example: PHP Program to find the sum of geometric series using a mathematical formula. PHP <?php function geometricSum($a, $r, $n) { if ($r == 1) { return $a * $n; } // Calculate the sum of geometric series $sum = $a * (1 - pow($r, $n)) / (1 - $r); return $sum; } // Driver code $a = 2; // First term $r = 3; // Common ratio $n = 4; // Number of terms echo "Sum of Geometric Series: " . geometricSum($a, $r, $n); ?> OutputSum of Geometric Series: 80 Comment More infoAdvertise with us Next Article PHP | Ds\Sequence sum() Function B blalverma92 Follow Improve Article Tags : PHP Geeks Premier League 2023 Similar Reads How to find Sum the Digits of a given Number in PHP ? We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to 2 min read PHP | Ds\Sequence sum() Function The Ds\Sequence::sum() function is an inbuilt function in PHP which is used to return the sum of all values of the sequence. Syntax: number abstract public Ds\Sequence::sum( void ) Parameters: This function does not accept any parameters. Return value: This function returns the sum of all sequence e 1 min read PHP | Ds\Sequence sum() Function The Ds\Sequence::sum() function is an inbuilt function in PHP which is used to return the sum of all values of the sequence. Syntax: number abstract public Ds\Sequence::sum( void ) Parameters: This function does not accept any parameters. Return value: This function returns the sum of all sequence e 1 min read PHP | Ds\Vector sum() Function The Ds\Vector::sum() function is an inbuilt function in PHP which returns the sum of all the elements of the vector. Syntax: number public Ds\Vector::sum( void ) Parameters: This function does not accept any parameter. Return Value: This function returns the sum of all elements in the vector. The su 2 min read PHP | Ds\Vector sum() Function The Ds\Vector::sum() function is an inbuilt function in PHP which returns the sum of all the elements of the vector. Syntax: number public Ds\Vector::sum( void ) Parameters: This function does not accept any parameter. Return Value: This function returns the sum of all elements in the vector. The su 2 min read PHP | Sum of digits of a number This is a simple PHP program where we need to calculate the sum of all digits of a number. Examples: Input : 711 Output : 9 Input : 14785 Output : 25 In this program, we will try to accept a number in the form of a string and then iterate through the length of the string. While iterating we will ext 1 min read Like