Program to print GP (Geometric Progression) - GeeksforGeeks

24 Nov, 2021
Given first term (a), common ratio (r) and a integer n of the Geometric Progression series, the task is to print th 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
Java
Python3
C#
PHP
Javascript
// 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 codeint 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 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.
# 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 numberr = 3 # Common ration = 5 # N th term to be find printGP(a, r, n) # This code is contributed by# Smitha Dinesh Semwal
// 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.
<?php// PHP program to print GP. // function to print GPfunction 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.?>
<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>
2 6 18 54 162
 
jit_t
PankajMishra10
surbhityagi15
rs1686740
series
Mathematical
School Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Modulo Operator (%) in C/C++ with Examples
Program to find GCD or HCF of two numbers
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java