📜  程序打印GP(Geometric Progression)

📅  最后修改于: 2021-05-04 14:01:47             🧑  作者: Mango

给定第一项(a),公共比率(r)和几何级数级数的整数n,任务是打印该级数的n个项。
例子:

Input : a = 2 r = 2, n = 4
Output : 2 4 8 16

方法 :

要打印“几何级数”系列,我们使用简单的公式。

TN = a1 * r(n-1)
CPP
// CPP program to print GP.
#include 
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.


PHP


Javascript


输出:
2 6 18 54 162