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.
so, we can say that geometric progression for nth term will be like −
gp1 = a1gp2 = a1 * r^(2-1)gp3 = a1 * r^(3-1). . .gpn = a1 * r^(n-1)
so the formula will be gp = a * r^(n-1).
exampleinput: a=1 r=2 n=5output: the 5th term of the series is: 16explanation: the terms will be 1, 2, 4, 8, 16 so the output will be 16input: a=1 r=2 n=8output: 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)stop
example#include <stdio.h>#include <math.h>//function to return the nth term of gpint nth_of_gp(int a, int r, int n) { // the nth term will be return( a * (int)(pow(r, n - 1)) );}//main blockint 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
以上就是c程序用于计算等比数列的第n项的详细内容。
