Program to print GP (Geometric Progression)
Last Updated :
29 Nov, 2023
Given first term (a), common ratio (r) and a integer n of the Geometric Progression series, the task is to print the n terms of the series.
Examples:
Input : a = 2 r = 2, n = 4
Output : 2 4 8 16
Approach :
We know the Geometric Progression series is like = 2, 4, 8, 16, 32 .......
In this series 2 is the starting term of the series .
Common ratio = 4 / 2 = 2 (ratio common in the series).
so we can write the series as :
t1 = a1
t2 = a1 * r(2-1)
t3 = a1 * r(3-1)
t4 = a1 * r(4-1)
.
.
.
.
tN = a1 * r(n-1)
To print the Geometric Progression series we use the simple formula .
TN = a1 * r(n-1)
CPP
// CPP program to print GP.
#include <bits/stdc++.h>
using namespace std;
void printGP(int a, int r, int n)
{
int curr_term;
for (int i = 0; i < n; i++) {
curr_term = a * pow(r, i);
cout << curr_term << " ";
}
}
// Driver code
int main()
{
int a = 2; // starting number
int r = 3; // Common ratio
int n = 5; // N th term to be find
printGP(a, r, n);
return 0;
}
Java
// Java program to print GP.
class GFG {
static void printGP(int a, int r, int n)
{
int curr_term;
for (int i = 0; i < n; i++) {
curr_term = a * (int)Math.pow(r, i);
System.out.print(curr_term + " ");
}
}
// Driver code
public static void main(String[] args)
{
int a = 2; // starting number
int r = 3; // Common ratio
int n = 5; // N th term to be find
printGP(a, r, n);
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python 3 program to print GP.
def printGP(a, r, n):
for i in range(0, n):
curr_term = a * pow(r, i)
print(curr_term, end =" ")
# Driver code
a = 2 # starting number
r = 3 # Common ratio
n = 5 # N th term to be find
printGP(a, r, n)
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# program to print GP.
using System;
class GFG {
static void printGP(int a, int r, int n)
{
int curr_term;
for (int i = 0; i < n; i++) {
curr_term = a * (int)Math.Pow(r, i);
Console.Write(curr_term + " ");
}
}
// Driver code
public static void Main()
{
int a = 2; // starting number
int r = 3; // Common ratio
int n = 5; // N th term to be find
printGP(a, r, n);
}
}
// This code is contributed by vt_m.
JavaScript
<script>
// JavaScript program to print GP.
function printGP(a, r, n)
{
let curr_term;
for (let i = 0; i < n; i++) {
curr_term = a * Math.pow(r, i);
document.write(curr_term + " ");
}
}
// Driver code
let a = 2; // starting number
let r = 3; // Common ratio
let n = 5; // N th term to be find
printGP(a, r, n);
// This code is contributed by Surbhi Tyagi
</script>
PHP
<?php
// PHP program to print GP.
// function to print GP
function printGP($a, $r, $n)
{
for ($i = 0; $i < $n; $i++)
{
$curr_term = $a * pow($r, $i);
echo $curr_term, " ";
}
}
// Driver Code
// starting number
$a = 2;
// Common ratio
$r = 3;
// N th term to be find
$n = 5;
printGP($a, $r, $n);
// This code is contributed by ajit.
?>
Time Complexity: O(nlog2n), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Approach 2: Using recursion to calculate each term of the GP and printing each term.
The printGP(int a, int r, int n) function takes three integer inputs a, r, and n, and recursively prints the first n terms of a geometric progression with first term a and common ratio r. If n is 0, the function returns without printing anything. Otherwise, it first calculates the current term currTerm using the formula currTerm = a * pow(r, n - 1), and then calls itself recursively with n-1 as the new input argument. Finally, it prints the current term on the console.
- In the main() function, we initialize the first term a to 2, common ratio r to 3, and the number of terms to print n to 5.
- We use cout to print the message "GP Series: " on the console.
- We call the printGP() function with a, r, and n as input arguments to recursively print the first n terms of the geometric progression.
- We use cout to insert a newline character after the output.
- Finally, we return 0 to indicate successful completion of the program.
- Overall, this code prints the first n terms of a geometric progression with given first term a, common ratio r, and number of terms to print n using a recursive function, and prints the result on the console.
C++
#include <iostream>
#include <cmath>
using namespace std;
void printGP(int a, int r, int n)
{
if (n == 0)
{
return;
}
int currTerm = a * pow(r, n - 1);
printGP(a, r, n - 1);
cout << currTerm << " ";
}
int main()
{
int a = 2; // first term
int r = 3; // common ratio
int n = 5; // number of terms
cout << "GP Series: ";
printGP(a, r, n);
cout << endl;
return 0;
}
Java
import java.lang.Math;
public class Main {
public static void printGP(int a, int r, int n) {
if (n == 0) {
return;
}
int currTerm = a * (int)Math.pow(r, n - 1);
printGP(a, r, n - 1);
System.out.print(currTerm + " ");
}
public static void main(String[] args) {
int a = 2; // first term
int r = 3; // common ratio
int n = 5; // number of terms
System.out.print("GP Series: ");
printGP(a, r, n);
System.out.println();
}
}
Python3
import math
def printGP(a, r, n):
if n == 0:
return
currTerm = a * pow(r, n - 1)
printGP(a, r, n - 1)
print(currTerm, end=" ")
a = 2 # first term
r = 3 # common ratio
n = 5 # number of terms
print("GP Series:", end=" ")
printGP(a, r, n)
print()
C#
using System;
public class Program
{
public static void Main()
{
int a = 2; // first term
int r = 3; // common ratio
int n = 5; // number of terms
Console.Write("GP Series: ");
PrintGP(a, r, n);
Console.WriteLine();
}
static void PrintGP(int a, int r, int n)
{
if (n == 0)
{
return;
}
int currTerm = a * (int)Math.Pow(r, n - 1);
PrintGP(a, r, n - 1);
Console.Write(currTerm + " ");
}
}
JavaScript
function printGP(a, r, n) {
// If n is 0, return nothing
if (n === 0) {
return;
}
// Calculate the current term
const currTerm = a * Math.pow(r, n - 1);
// Recursively call printGP with n - 1 until n is 0
printGP(a, r, n - 1);
// Print the current term
console.log(currTerm + " ");
}
const a = 2; // first term
const r = 3; // common ratio
const n = 5; // number of terms
console.log("GP Series: ");
printGP(a, r, n);
console.log("");
OutputGP Series: 2 6 18 54 162
Time complexity :- O(N)
Space complexity :- O(N)
Approach 3(Using Loop): To solve the problem follow the below idea:
- Initialize a counter variable i to 0.
- Use a while loop to iterate over the first n terms of the series, printing out each term and multiplying the previous term by the common ratio to get the next term.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void printGP(int a, int r, int n) {
for (int i = 0; i < n; i++) {
cout << a << " ";
a *= r;
}
}
int main() {
// Starting number
int a = 2;
// Common ratio
int r = 3;
// Nth term to be found
int n = 5;
cout << "GP Series: ";
// Function call
printGP(a, r, n);
return 0;
}
Java
// java program to find nth term
// of geometric progression
import java.io.*;
import java.lang.*;
class GFG {
public static void printGP(int a, int r, int n)
{
int i = 0;
while (i < n) {
System.out.print(a + " ");
a *= r;
i++;
}
}
// Driver code
public static void main(String[] args)
{
// starting number
int a = 2;
// Common ratio
int r = 3;
// N th term to be find
int n = 5;
System.out.print("GP Series: ");
// Function call
printGP(a, r, n);
}
}
Python
# Function to print geometric progression (GP) series
def print_gp(a, r, n):
for i in range(n):
print(a),
a *= r
# Main function
def main():
# Starting number
a = 2
# Common ratio
r = 3
# Nth term to be found
n = 5
print("GP Series: "),
# Function call
print_gp(a, r, n)
# Run the main function
if __name__ == "__main__":
main()
C#
using System;
class Program
{
static void PrintGP(int a, int r, int n)
{
for (int i = 0; i < n; i++)
{
Console.Write(a + " ");
a *= r;
}
}
static void Main()
{
// Starting number
int a = 2;
// Common ratio
int r = 3;
// Nth term to be found
int n = 5;
Console.Write("GP Series: ");
// Function call
PrintGP(a, r, n);
}
}
JavaScript
function printGP(a, r, n) {
let i = 0;
while (i < n) {
// Print the current term
process.stdout.write(a + " ");
a *= r;
i++;
}
}
// Starting number
const a = 2;
// Common ratio
const r = 3;
// Nth term to be found
const n = 5;
process.stdout.write("GP Series: ");
// Function call
printGP(a, r, n);
OutputGP Series: 2 6 18 54 162
Time complexity: O(N)
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem