
Given ‘a’ the First term, ‘r’ the common ratio and ‘n’ for the number of terms in a series. The task is to find the nth term of the series.
So, before discussing how to write a program for the problem first we should know what is Geometric Progression.
Geometric progression or Geometric sequence in mathematics are where each term after the first term is found by multiplying the previous one with the common ratio for a fixed number of terms.
Like 2, 4, 8, 16, 32.. is a geometric progression with first term 2 and common ratio 2. If we have n = 4 then the output will be 16.
MATLAB(矩阵实验室)是MATrix LABoratory的缩写,是一款由美国The MathWorks公司出品的商业数学软件。MATLAB是一种用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境。除了矩阵运算、绘制函数/数据图像等常用功能外,MATLAB还可以用来创建用户界面及与调用其它语言(包括C,C++和FORTRAN)编写的程序。MATLAB基础知识;命令窗口是用户与MATLAB进行交互作业的主要场所,用户输入的MATLAB交互命令均在命令窗口执行。 感兴趣的朋友可以
So, we can say that Geometric Progression for nth term will be like −
GP1 = a1 GP2 = a1 * r^(2-1) GP3 = a1 * r^(3-1) . . . GPn = a1 * r^(n-1)
So the formula will be GP = a * r^(n-1).
Example
Input: A=1 R=2 N=5 Output: The 5th term of the series is: 16 Explanation: The terms will be 1, 2, 4, 8, 16 so the output will be 16 Input: A=1 R=2 N=8 Output: The 8th Term of the series is: 128
我们将使用的方法来解决给定的问题 −
- 取第一项A,公比R,以及序列的数量N。
- 然后通过 A * (int)(pow(R, N - 1) 计算第n项。
- 返回上述计算得到的输出。
算法
Start
Step 1 -> In function int Nth_of_GP(int a, int r, int n)
Return( a * (int)(pow(r, n - 1))
Step 2 -> In function int main()
Declare and set a = 1
Declare and set r = 2
Declare and set n = 8
Print The output returned from calling the function Nth_of_GP(a, r, n)
StopExample
#include#include //function to return the nth term of GP int Nth_of_GP(int a, int r, int n) { // the Nth term will be return( a * (int)(pow(r, n - 1)) ); } //Main Block int main() { // initial number int a = 1; // Common ratio int r = 2; // N th term to be find int n = 8; printf("The %dth term of the series is: %d ",n, Nth_of_GP(a, r, n) ); return 0; }
输出
The 8th term of the series is: 128









