您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

C程序用于计算等比数列的第N项

2026/1/10 12:44:38发布19次查看
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.
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项的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product