GP Term


Sun Feb 06 2022 01:54:52 GMT+0000 (UTC)

Saved by @Uttam #java #mathematics #gfg #geeksforgeeks #gpterm #geometricprogressionterm #geometricseries

import java.util.*;

class Solution
    public double termOfGP(int A,int B,int N)
        // common ratio is given by r=b/a
        double r=(double)B/(double)A;
        // Nth term is given by a(r^(N-1))
        return (A*Math.pow(r,N-1)); 


public class Main {
	public static void main (String[] args) {
	    //taking input using Scanner class
		Scanner sc=new Scanner(;
		//taking total testcases
		int T=sc.nextInt();
		    Solution  obj=new Solution ();
		    int A,B;
		    //taking A and B
		    int N;
		    //taking N
		   //calling method termOfGP() of class GP

6. GP Term Given the first 2 terms A and B of a Geometric Series. The task is to find the Nth term of the series. Example 1: Input: A = 2 B = 3 N = 1 Output: 2 Explanation: The first term is already given in the input as 2 Example 2: Input: A = 1 B = 2 N = 2 Output: 2 Explanation: Common ratio = 2, Hence second term is 2. Your Task: You don't need to read input or print anything. Your task is to complete the function termOfGP() that takes A, B and N as parameter and returns Nth term of GP. The return value should be double that would be automatically converted to floor by the driver code. Expected Time Complexity: O(logN) Expected Auxilliary Space: O(1) Constraints: -100 <= A <= 100 -100 <= B <= 100 1 <= N <= 5