Find N-th element in geometric progression series

Here given code implementation process.

// C program 
// Find N-th element in geometric progression series
#include <stdio.h>

#include <math.h>

// Find nth term of geometric progression
// a : starting point
// ratio : common ratio
// n : nth element
void nth_gp_element(double a, double ratio, int n)
{
    printf("\n [ Start : %lf, Ratio : %lf, N-th : %d ] ", a, ratio, n);
    //Find nth element in GP
    double result = a * (pow(ratio, n - 1));
    printf("\n Result : %lf", result);
}
// Driver code 
int main()
{
    // Test Cases
    nth_gp_element(3, 2, 6);
    nth_gp_element(2, 3, 7);
    nth_gp_element(4.3, 3, 3);
    return 0;
}

Output

 [ Start : 3.000000, Ratio : 2.000000, N-th : 6 ]
 Result : 96.000000
 [ Start : 2.000000, Ratio : 3.000000, N-th : 7 ]
 Result : 1458.000000
 [ Start : 4.300000, Ratio : 3.000000, N-th : 3 ]
 Result : 38.700000
// Java program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	public void nth_gp_element(double a, double ratio, int n)
	{
		System.out.print("\n [ Start : " + a + ", Ratio : " + ratio + ", N-th : " + n + " ] ");
		//Find nth element in GP
		double result = a * (Math.pow(ratio, n - 1));
		System.out.print("\n Result : " + result);
	}
	public static void main(String[] args)
	{
		MyMath obj = new MyMath();
		// Test Cases
		obj.nth_gp_element(3, 2, 6);
		obj.nth_gp_element(2, 3, 7);
		obj.nth_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 3.0, Ratio : 2.0, N-th : 6 ]
 Result : 96.0
 [ Start : 2.0, Ratio : 3.0, N-th : 7 ]
 Result : 1458.0
 [ Start : 4.3, Ratio : 3.0, N-th : 3 ]
 Result : 38.699999999999996
//Include header file
#include <iostream>
#include<math.h>
using namespace std;

// C++ program
// Find N-th element in geometric progression series

class MyMath
{
	public:
		// Find nth term of geometric progression
		// a : starting point
		// ratio : common ratio
		// n : nth element
		void nth_gp_element(double a, double ratio, int n)
		{
			cout << "\n [ Start : " << a << ", Ratio : " << ratio << ", N-th : " << n << " ] ";
			//Find nth element in GP
			double result = a * (pow(ratio, n - 1));
			cout << "\n Result : " << result;
		}
};
int main()
{
	MyMath obj = MyMath();
	// Test Cases
	obj.nth_gp_element(3, 2, 6);
	obj.nth_gp_element(2, 3, 7);
	obj.nth_gp_element(4.3, 3, 3);
	return 0;
}

Output

 [ Start : 3, Ratio : 2, N-th : 6 ]
 Result : 96
 [ Start : 2, Ratio : 3, N-th : 7 ]
 Result : 1458
 [ Start : 4.3, Ratio : 3, N-th : 3 ]
 Result : 38.7
//Include namespace system
using System;
// C# program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	public void nth_gp_element(double a, double ratio, int n)
	{
		Console.Write("\n [ Start : " + a + ", Ratio : " + ratio + ", N-th : " + n + " ] ");
		//Find nth element in GP
		double result = a * (Math.Pow(ratio, n - 1));
		Console.Write("\n Result : " + result);
	}
	public static void Main(String[] args)
	{
		MyMath obj = new MyMath();
		// Test Cases
		obj.nth_gp_element(3, 2, 6);
		obj.nth_gp_element(2, 3, 7);
		obj.nth_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 3, Ratio : 2, N-th : 6 ]
 Result : 96
 [ Start : 2, Ratio : 3, N-th : 7 ]
 Result : 1458
 [ Start : 4.3, Ratio : 3, N-th : 3 ]
 Result : 38.7
<?php
// Php program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	public	function nth_gp_element($a, $ratio, $n)
	{
		echo "\n [ Start : ". $a .", Ratio : ". $ratio .", N-th : ". $n ." ] ";
		//Find nth element in GP
		$result = $a * (pow($ratio, $n - 1));
		echo "\n Result : ". $result;
	}
}

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

Output

 [ Start : 3, Ratio : 2, N-th : 6 ]
 Result : 96
 [ Start : 2, Ratio : 3, N-th : 7 ]
 Result : 1458
 [ Start : 4.3, Ratio : 3, N-th : 3 ]
 Result : 38.7
// Node Js program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	nth_gp_element(a, ratio, n)
	{
		process.stdout.write("\n [ Start : " + a + ", Ratio : " + ratio + ", N-th : " + n + " ] ");
		//Find nth element in GP
		var result = a * (Math.pow(ratio, n - 1));
		process.stdout.write("\n Result : " + result);
	}
}

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

Output

 [ Start : 3, Ratio : 2, N-th : 6 ]
 Result : 96
 [ Start : 2, Ratio : 3, N-th : 7 ]
 Result : 1458
 [ Start : 4.3, Ratio : 3, N-th : 3 ]
 Result : 38.699999999999996
#  Python 3 program
#  Find N-th element in geometric progression series

class MyMath :
	#  Find nth term of geometric progression
	#  a : starting point
	#  ratio : common ratio
	#  n : nth element
	def nth_gp_element(self, a, ratio, n) :
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N-th : ", n ," ] ", end = "")
		# Find nth element in GP
		result = a * (ratio**(n - 1))
		print("\n Result : ", result, end = "")
	

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

if __name__ == "__main__": main()

Output

 [ Start :  3 , Ratio :  2 , N-th :  6  ]
 Result :  96
 [ Start :  2 , Ratio :  3 , N-th :  7  ]
 Result :  1458
 [ Start :  4.3 , Ratio :  3 , N-th :  3  ]
 Result :  38.699999999999996
#  Ruby program
#  Find N-th element in geometric progression series

class MyMath

	#  Find nth term of geometric progression
	#  a : starting point
	#  ratio : common ratio
	#  n : nth element
	def nth_gp_element(a, ratio, n)
	
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N-th : ", n ," ] ")
		# Find nth element in GP
		result = a * (ratio**(n - 1))
		print("\n Result : ", result)
	end
end
def main()

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

Output

 [ Start : 3, Ratio : 2, N-th : 6 ] 
 Result : 96
 [ Start : 2, Ratio : 3, N-th : 7 ] 
 Result : 1458
 [ Start : 4.3, Ratio : 3, N-th : 3 ] 
 Result : 38.699999999999996
// Scala program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	def nth_gp_element(a: Double, ratio: Double, n: Int): Unit = {
		print("\n [ Start : " + a + ", Ratio : " + ratio + ", N-th : " + n + " ] ");
		//Find nth element in GP
		var result: Double = a * (Math.pow(ratio, n - 1));
		print("\n Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMath = new MyMath();
		// Test Cases
		obj.nth_gp_element(3, 2, 6);
		obj.nth_gp_element(2, 3, 7);
		obj.nth_gp_element(4.3, 3, 3);
	}
}

Output

 [ Start : 3.0, Ratio : 2.0, N-th : 6 ]
 Result : 96.0
 [ Start : 2.0, Ratio : 3.0, N-th : 7 ]
 Result : 1458.0
 [ Start : 4.3, Ratio : 3.0, N-th : 3 ]
 Result : 38.699999999999996
import Foundation

// Swift program
// Find N-th element in geometric progression series
class MyMath
{
	// Find nth term of geometric progression
	// a : starting point
	// ratio : common ratio
	// n : nth element
	func nth_gp_element(_ a: Double, _ ratio: Double, _ n: Int)
	{
		print("\n [ Start : ", a ,", Ratio : ", ratio ,", N-th : ", n ," ] ", terminator: "");
		//Find nth element in GP
		let result: Double = a * (pow(ratio, Double(n - 1)));
		print("\n Result : ", result, terminator: "");
	}
}
func main()
{
	let obj: MyMath = MyMath();
	// Test Cases
	obj.nth_gp_element(3, 2, 6);
	obj.nth_gp_element(2, 3, 7);
	obj.nth_gp_element(4.3, 3, 3);
}
main();

Output

 [ Start :  3.0 , Ratio :  2.0 , N-th :  6  ]
 Result :  96.0
 [ Start :  2.0 , Ratio :  3.0 , N-th :  7  ]
 Result :  1458.0
 [ Start :  4.3 , Ratio :  3.0 , N-th :  3  ]
 Result :  38.7


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