×

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth term that needs to be calculated.

Example1

Input

int num1 = 2, // first term

int num2 = 2 // common ratio

int num3 = 4 // 4th term to be found

Output: The fourth term is 16.

Explanation: To calculate the following term in a geometric progression, multiply the current term by the common ratio. Therefore, the second term will be as follows if the first term is the present term:

secondTerm = a1 x a2 = 2 x 2 = 4, using the second term we can compute the third term, and so on.

thirdTerm = secondTerm x common ratio = 4 x 2 = 8

fourthTerm = thirdTerm x common ratio = 8 x 2 = 16

Thus, we get the fourth term as 16.

Example2

Input

int num1 = 3, // first term

int num2 = 3 // common ratio

int num3 = 3 //third term to be found

Output: The third term is 27.

Brute Force Approach

The idea is to utilise the formula axe r to calculate the nth term of the geometric progression (n - 1). The magnitude of r (n - 1)

Nth Term of Geometric Progression Program in Java

public class NthtermGP1  
{  
  
public long nth_term_of_GP(int num, int first_term, int common_ratio)   
{  
  
long ans = (first_term * power(common_ratio, num - 1)) ;  
  
return  ans;  
  
}  
  
public long power(int common_ratio, int num)   
{  
  
  
if(num == 0)   
{  
return 1;  
}  
  
int j1 = 0;  
  
long power = 1;   
  
while(j1 < num)   
{  
power = (power * common_ratio);  
j1 = j1 + 1;  
}  
  
return power;  
  
}  
  


public static void main(String argvs[])  
{  
    
int first_term = 2;  
int common_ratio = 2;  
int nth_term = 4;  


  
NthtermGP1 object1 = new NthtermGP1();  
long nth_term1 = object1.nth_term_of_GP(nth_term, first_term, common_ratio);  
System.out.print("For a geometric progression which has the ");  
System.out.print("first term as: " + first_term + " and the common ratio as: " + common_ratio);  
System.out.print(", the " + nth_term + "th term is: " + nth_term1);  
System.out.println( "\n" );  
first_term = 3;  
common_ratio = 3;  
nth_term = 3;  
nth_term1 = object1.nth_term_of_GP(nth_term, first_term, common_ratio);  
System.out.print("For a geometric progression which has the ");  
System.out.print("first term as: " + first_term + " and the common ratio as: " + common_ratio);  
System.out.print(", the " + nth_term + "rd term is: " + nth_term1);  
}  
}  

Output

N-th Term of Geometric Progression in Java

Complexity analysis: Because r(n - 1) is calculated using a while loop, the program's time complexity is O(n), where n is the number that needs to be calculated. Since the programme uses no more space, its space complexity is O(1).

We can use additional optimization to speed up the process of computing the value of r. (n - 1).

Recursive Methodology

NthtermGP2 .java

public class NthtermGP2   
{  
  
public long nth_term_of_GP(int num, int first_term, int common_ratio)   
{  
  
long ans = (first_term * power(common_ratio, num - 1)) ;  
  
return  ans;  
  
}  
  


public long power(int common_ratio, int num)   
{  
  


if(num == 0)  
{  
return 1;  
}  
  


long temp = power(common_ratio, num / 2);  


if(num % 2 == 0)   
{  
return (temp * temp);  
}  
else   
{  
return ((temp * temp) * common_ratio);  
}  
  
}  
  


public static void main(String argvs[])  
{  


int first_term = 2;  
int common_ratio = 2;  
int nth_term = 4;  
  
  
NthtermGP2 object1 = new NthtermGP2();  
long nth_term1 = object1.nth_term_of_GP(nth_term, first_term, common_ratio);  
System.out.print("For a geometric progression which has the ");  
System.out.print("first term as: " + first_term + " and the common ratio as: " + common_ratio);  
System.out.print(", the " + nth_term + "th term is: " + nth_term1);  
System.out.println( "\n" );  
  


first_term = 3;  
common_ratio = 3;  
nth_term = 3;  
  
nth_term1 = object1.nth_term_of_GP(nth_term, first_term, common_ratio);  
System.out.print("For a geometric progression which has the ");  
System.out.print("first term as: " + first_term + " and the common ratio as: " + common_ratio);  
System.out.print(", the " + nth_term + "rd term is: " + nth_term1);  
  
  
}  
  
}  

Output

N-th Term of Geometric Progression in Java

NthtermGP3.java

import java.io.*;
import java.lang.*;


class NthtermGP3 {
public static int NthTermOfGP(int num, int ratio, int N1)
{

return (num * (int)(Math.pow(ratio, N1 - 1)));
}



public static void main(String[] args)
{

int num = 2;



int ratio = 2;



int N1 = 4;



System.out.print("The " + N1 + "th term of the"
+ " series is : "
+ NthTermOfGP(num, ratio, N1));
}
}

Output

N-th Term of Geometric Progression in Java

Related Topics

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Java Math rint() Method

The rint() method of Java Math class returns the double value which is close to the specified argument and is equal to mathematical integer. Syntax: public static double rint(double a) Parameters: The parameter ‘a’...

1 minute read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

Java Math log() Method

The log() method of Math class returns the natural logarithmic value for the specified double argument. Syntax: public static double log(double a) Parameters: The parameter ‘a’ represents the value. Return Value: The log() method returns the...

1 minute read.

Java String startsWith() method

Java String startsWith() method checks whether current String starts with given prefix or not . Syntax: public boolean startsWith(String prefix) public boolean startsWith(String prefix, int offset) Parameters: prefix : It is sequence of character Returns: It returns...

2 minutes read.

Java Integer toOctalString() method

The toOctalString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 8. Syntax public static String toOctalString (int  i) Parameters The parameter ‘i’ represents...

1 minute read.

Sum of digits in string in java

To find the sum of all digits in a string, you need to traverse through the string one by one character; if the character is an integer, you need to...

2 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Contextual keywords in Java

Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar. These are the keywords in the code...

3 minutes read.

Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int. Syntax: public static int toIntExact (long value) Parameters: The...

2 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

Dangling Else problem in Java

A language interpretation uncertainty is the hanging other issue. The following two types of condition executed code are both possible in programming: 1. if-then-else form 2. if-then form When dealing with the nested...

3 minutes read.