Find the sum of geometric series

Here given code implementation process.

// C program 
// Find the sum of geometric series
#include <stdio.h>

#include <math.h>

// Sum of geometric progression series
// a : starting point
// ratio : common ratio
// n : number of element
void sum_gp_element(double a, double ratio, int n)
{
	printf("\n [ Start : %lf, Ratio : %lf, N : %d ] ", a, ratio, n);
	//Calcuate sum 
	double sum = (a * (1 - (pow(ratio, n)))) / (1 - ratio);
	printf("\n Sum : %lf\n", sum);
}
// Driver code 
int main()
{
	// Test Cases
	sum_gp_element(6, 2, 6);
	sum_gp_element(4, 3, 7);
	sum_gp_element(4.3, 3, 3);
	return 0;
}
//compile the code like this
// compile : gcc -o a test.c -1m 
// here test.c is program file
// run : ./a.out

Output

 [ Start : 6.000000, Ratio : 2.000000, N : 6 ]
 Sum : 378.000000

 [ Start : 4.000000, Ratio : 3.000000, N : 7 ]
 Sum : 4372.000000

 [ Start : 4.300000, Ratio : 3.000000, N : 3 ]
 Sum : 55.900000
// Java program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	public void sum_gp_element(double a, double ratio, int n)
	{
		System.out.print("\n [ Start : " + a + ", Ratio : " + ratio + ", N : " + n + " ] ");
		//Calcuate sum 
		double sum = (a * (1 - (Math.pow(ratio, n)))) / (1 - ratio);
		System.out.print("\n Sum : " + sum + "\n");
	}
	public static void main(String[] args)
	{
		MyMath obj = new MyMath();
		// Test Cases
		obj.sum_gp_element(6, 2, 6);
		obj.sum_gp_element(4, 3, 7);
		obj.sum_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 6.0, Ratio : 2.0, N : 6 ]
 Sum : 378.0

 [ Start : 4.0, Ratio : 3.0, N : 7 ]
 Sum : 4372.0

 [ Start : 4.3, Ratio : 3.0, N : 3 ]
 Sum : 55.9
//Include header file
#include <iostream>

#include<math.h>

using namespace std;
// C++ program
// Find the sum of geometric series
class MyMath
{
	public:
		// Sum of geometric progression series
		// Here
		// a : starting point
		// ratio : common ratio
		// n : number of element
		void sum_gp_element(double a, double ratio, int n)
		{
			cout << "\n [ Start : " << a << ", Ratio : " << ratio << ", N : " << n << " ] ";
			//Calcuate sum 
			double sum = (a * (1 - (pow(ratio, n)))) / (1 - ratio);
			cout << "\n Sum : " << sum << "\n";
		}
};
int main()
{
	MyMath obj = MyMath();
	// Test Cases
	obj.sum_gp_element(6, 2, 6);
	obj.sum_gp_element(4, 3, 7);
	obj.sum_gp_element(4.3, 3, 3);
	return 0;
}

Output

 [ Start : 6, Ratio : 2, N : 6 ]
 Sum : 378

 [ Start : 4, Ratio : 3, N : 7 ]
 Sum : 4372

 [ Start : 4.3, Ratio : 3, N : 3 ]
 Sum : 55.9
//Include namespace system
using System;
// C# program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	public void sum_gp_element(double a, double ratio, int n)
	{
		Console.Write("\n [ Start : " + a + ", Ratio : " + ratio + ", N : " + n + " ] ");
		//Calcuate sum 
		double sum = (a * (1 - (Math.Pow(ratio, n)))) / (1 - ratio);
		Console.Write("\n Sum : " + sum + "\n");
	}
	public static void Main(String[] args)
	{
		MyMath obj = new MyMath();
		// Test Cases
		obj.sum_gp_element(6, 2, 6);
		obj.sum_gp_element(4, 3, 7);
		obj.sum_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 6, Ratio : 2, N : 6 ]
 Sum : 378

 [ Start : 4, Ratio : 3, N : 7 ]
 Sum : 4372

 [ Start : 4.3, Ratio : 3, N : 3 ]
 Sum : 55.9
<?php
// Php program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	public	function sum_gp_element($a, $ratio, $n)
	{
		echo "\n [ Start : ". $a .", Ratio : ". $ratio .", N : ". $n ." ] ";
		//Calcuate sum 
		$sum = ($a * (1 - (pow($ratio, $n)))) / (1 - $ratio);
		echo "\n Sum : ". $sum ."\n";
	}
}

function main()
{
	$obj = new MyMath();
	// Test Cases
	$obj->sum_gp_element(6, 2, 6);
	$obj->sum_gp_element(4, 3, 7);
	$obj->sum_gp_element(4.3, 3, 3);
}
main();

Output

 [ Start : 6, Ratio : 2, N : 6 ]
 Sum : 378

 [ Start : 4, Ratio : 3, N : 7 ]
 Sum : 4372

 [ Start : 4.3, Ratio : 3, N : 3 ]
 Sum : 55.9
// Node Js program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	sum_gp_element(a, ratio, n)
	{
		process.stdout.write("\n [ Start : " + a + ", Ratio : " + ratio + ", N : " + n + " ] ");
		//Calcuate sum 
		var sum = (a * (1 - (Math.pow(ratio, n)))) / (1 - ratio);
		process.stdout.write("\n Sum : " + sum + "\n");
	}
}

function main()
{
	var obj = new MyMath();
	// Test Cases
	obj.sum_gp_element(6, 2, 6);
	obj.sum_gp_element(4, 3, 7);
	obj.sum_gp_element(4.3, 3, 3);
}
main();

Output

 [ Start : 6, Ratio : 2, N : 6 ]
 Sum : 378

 [ Start : 4, Ratio : 3, N : 7 ]
 Sum : 4372

 [ Start : 4.3, Ratio : 3, N : 3 ]
 Sum : 55.9
#  Python 3 program
#  Find the sum of geometric series

class MyMath :
	#  Sum of geometric progression series
	#  Here
	#  a : starting point
	#  ratio : common ratio
	#  n : number of element
	def sum_gp_element(self, a, ratio, n) :
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N : ", n ," ] ", end = "")
		# Calcuate sum 
		sum = (a * (1 - ((ratio**n)))) / (1 - ratio)
		print("\n Sum : ", sum ,"\n", end = "")
	

def main() :
	obj = MyMath()
	#  Test Cases
	obj.sum_gp_element(6, 2, 6)
	obj.sum_gp_element(4, 3, 7)
	obj.sum_gp_element(4.3, 3, 3)

if __name__ == "__main__": main()

Output

 [ Start :  6 , Ratio :  2 , N :  6  ]
 Sum :  378.0

 [ Start :  4 , Ratio :  3 , N :  7  ]
 Sum :  4372.0

 [ Start :  4.3 , Ratio :  3 , N :  3  ]
 Sum :  55.9
#  Ruby program
#  Find the sum of geometric series
class MyMath

	#  Sum of geometric progression series
	#  Here
	#  a : starting point
	#  ratio : common ratio
	#  n : number of element
	def sum_gp_element(a, ratio, n)
	
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N : ", n ," ] ")
		# Calcuate sum 
		sum = (a * (1 - (ratio**n))) / (1 - ratio)
		print("\n Sum : ", sum ,"\n")
	end
end
def main()

	obj = MyMath.new()
	#  Test Cases
	obj.sum_gp_element(6, 2, 6)
	obj.sum_gp_element(4, 3, 7)
	obj.sum_gp_element(4.3, 3, 3)
end
main()

Output

 [ Start : 6, Ratio : 2, N : 6 ] 
 Sum : 378

 [ Start : 4, Ratio : 3, N : 7 ] 
 Sum : 4372

 [ Start : 4.3, Ratio : 3, N : 3 ] 
 Sum : 55.9
// Scala program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	def sum_gp_element(a: Double, ratio: Double, n: Int): Unit = {
		print("\n [ Start : " + a + ", Ratio : " + ratio + ", N : " + n + " ] ");
		//Calcuate sum 
		var sum: Double = a * (1 - (Math.pow(ratio, n))) / (1 - ratio);
		print("\n Sum : " + sum + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMath = new MyMath();
		// Test Cases
		obj.sum_gp_element(6, 2, 6);
		obj.sum_gp_element(4, 3, 7);
		obj.sum_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 6.0, Ratio : 2.0, N : 6 ]
 Sum : 378.0

 [ Start : 4.0, Ratio : 3.0, N : 7 ]
 Sum : 4372.0

 [ Start : 4.3, Ratio : 3.0, N : 3 ]
 Sum : 55.9
import Foundation
// Swift program
// Find the sum of geometric series
class MyMath
{
	// Sum of geometric progression series
	// Here
	// a : starting point
	// ratio : common ratio
	// n : number of element
	func sum_gp_element(_ a: Double, _ ratio: Double, _ n: Int)
	{
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N : ", n ," ] ", terminator: "");
		//Calcuate sum 
		let sum: Double = (a * (1 - (pow(ratio, Double(n))))) / (1 - ratio);
		print("\n Sum : ", sum ,"\n", terminator: "");
	}
}
func main()
{
	let obj: MyMath = MyMath();
	// Test Cases
	obj.sum_gp_element(6, 2, 6);
	obj.sum_gp_element(4, 3, 7);
	obj.sum_gp_element(4.3, 3, 3);
}
main();

Output

 [ Start :  6.0 , Ratio :  2.0 , N :  6  ]
 Sum :  378.0

 [ Start :  4.0 , Ratio :  3.0 , N :  7  ]
 Sum :  4372.0

 [ Start :  4.3 , Ratio :  3.0 , N :  3  ]
 Sum :  55.9


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved