×

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

Types of Garbage Collector in Java

Garbage collection is a Java feature that offers automatic memory management. The JVM is in charge of it. The programmer does not have to handle object creation and deallocation. We...

3 minutes read.

Java Integer numberOfLeadingZeros() method

The numberOfLeadingZeros()  method of Java Integer class returns the total number of zero bits preceding the highest-order one-bit in the 2’s complement binary representation of the specified int value.  Syntax public static...

1 minute read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

Java String contains() method

contains() method returns true only if it contains the given sequence of characters otherwise it returns false. syntax: public boolean contains(CharSequence sequence) parameters: sequence : It is the sequence to be searched. Returns: It returns true...

1 minute read.

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.

Sphenic Number in Java

In this section, we will learn what is a sphenic number is and show you how to write Java programmes to determine if a specific number are sphenic or not....

3 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Bellman Ford Algorithm in Java

Numerous algorithms have been used in dynamic programming to determine the shortest path inside a graph. Among them are Floyd, all-pair shortest path problem, Breadth First Search, Depth First Search,...

6 minutes read.

Java Enum Keyword

Definition: A data type in Java called Enum has a respect to supply of constants. The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST),...

4 minutes read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

Java Boolean getBoolean() Method

The getBoolean() method of Java Boolean class returns true if the specified system property is not null and is equal to the String ‘true’, else the result returned is false. Syntax public...

1 minute read.

How to Create Different Packages for Different Classes in Java

Packages in Java In Java, Packages are an assortment of classes, sub-packages, and connection points. i.e. A package addresses a word reference that contains a connected gathering of styles and points...

7 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.