GP Term

PHOTO EMBED

Sun Feb 06 2022 01:54:52 GMT+0000 (Coordinated Universal Time)

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

import java.io.*;
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(System.in);
		
		//taking total testcases
		int T=sc.nextInt();
		while(T-->0)
		{
		    
		    Solution  obj=new Solution ();
		    int A,B;
		    
		    //taking A and B
		    A=sc.nextInt();
		    B=sc.nextInt();
		    int N;
		    
		    //taking N
		    N=sc.nextInt();
		    
		   //calling method termOfGP() of class GP
		   System.out.println((int)Math.floor(obj.termOfGP(A,B,N)));
		    
		}
		
	}
}
content_copyCOPY

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

https://practice.geeksforgeeks.org/problems/gp-term/1/?track=DSASP-Mathematics&batchId=190