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
#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 << " " ;
}
}
int main()
{
int a = 2;
int r = 3;
int n = 5;
printGP(a, r, n);
return 0;
}
|
Java
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 + " " );
}
}
public static void main(String[] args)
{
int a = 2 ;
int r = 3 ;
int n = 5 ;
printGP(a, r, n);
}
}
|
Python3
def printGP(a, r, n):
for i in range ( 0 , n):
curr_term = a * pow (r, i)
print (curr_term, end = " " )
a = 2
r = 3
n = 5
printGP(a, r, n)
|
Javascript
<script>
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 + " " );
}
}
let a = 2;
let r = 3;
let n = 5;
printGP(a, r, n);
</script>
|
C#
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 + " " );
}
}
public static void Main()
{
int a = 2;
int r = 3;
int n = 5;
printGP(a, r, n);
}
}
|
PHP
<?php
function printGP( $a , $r , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
$curr_term = $a * pow( $r , $i );
echo $curr_term , " " ;
}
}
$a = 2;
$r = 3;
$n = 5;
printGP( $a , $r , $n );
?>
|
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;
int r = 3;
int n = 5;
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 ;
int r = 3 ;
int n = 5 ;
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
r = 3
n = 5
print ( "GP Series:" , end = " " )
printGP(a, r, n)
print ()
|
C#
using System;
public class Program
{
public static void Main()
{
int a = 2;
int r = 3;
int n = 5;
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 === 0) {
return ;
}
const currTerm = a * Math.pow(r, n - 1);
printGP(a, r, n - 1);
console.log(currTerm + " " );
}
const a = 2;
const r = 3;
const n = 5;
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:
Java
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++;
}
}
public static void main(String[] args)
{
int a = 2 ;
int r = 3 ;
int n = 5 ;
System.out.print( "GP Series: " );
printGP(a, r, n);
}
}
|
OutputGP Series: 2 6 18 54 162
Time complexity: O(N)
Auxiliary Space: O(1)