📌  相关文章
📜  几何级数级数的第N个程序

📅  最后修改于: 2021-05-05 01:11:02             🧑  作者: Mango

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

Input : a = 2 r = 2, N = 4
Output :
The 4th term of the series is : 16

Input : a = 2 r = 3, N = 5
Output :
The 5th term of the series is : 162

方法:

要找到“几何级数”系列中的第N项,我们使用简单的公式。

TN = a1 * r(N-1)
C++
// CPP Program to find nth term of
// geometric progression
#include 
  
using namespace std;
 
int Nth_of_GP(int a, int r, int N)
{
    // using formula to find
    // the Nth term
    // TN = a1 * r(N-1)
    return( a * (int)(pow(r, N - 1)) );
     
}
 
// Driver code
int main()
{
    // starting number
    int a = 2;
     
    // Common ratio
    int r = 3;
     
    // N th term to be find
    int N = 5;
     
    // Display the output
    cout << "The "<< N <<"th term of the series is : "
        << Nth_of_GP(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 int Nth_of_GP(int a,
                                int r,
                                int N)
    {
        // using formula to find the Nth
        // term TN = a1 * r(N-1)
        return ( a * (int)(Math.pow(r, N - 1)) );
    }
 
    // 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;
 
        // Display the output
        System.out.print("The "+ N + "th term of the" +
                " series is : " + Nth_of_GP(a, r, N));
    }
}


Python3
# Python3 Program to find nth
# term of geometric progression
import math
 
def Nth_of_GP(a, r, N):
 
    # Using formula to find the Nth
    # term TN = a1 * r(N-1)
    return( a * (int)(math.pow(r, N - 1)) )
     
# Driver code
a = 2 # Starting number
r = 3 # Common ratio
N = 5 # N th term to be find
     
print("The", N, "th term of the series is :",
                            Nth_of_GP(a, r, N))
 
 
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to find nth term
// of geometric progression
using System;
 
class GFG
{
     
    public static int Nth_of_GP(int a,
                                int r,
                                int N)
    {
         
        // using formula to find the Nth
        // term TN = a1 * r(N-1)
        return ( a * (int)(Math.Pow(r, N - 1)) );
    }
 
    // Driver code
    public static void Main()
    {
        // starting number
        int a = 2;
         
        // Common ratio
        int r = 3;
         
        // N th term to be find
        int N = 5;
 
        // Display the output
        Console.Write("The "+ N + "th term of the" +
            " series is : " + Nth_of_GP(a, r, N));
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


输出 :

The 5th term of the series is : 162