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

c程序用于计算等比数列的第n项

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).

Example

Input: 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 #include //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项的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2582234.html

(0)
上一篇 2025年3月6日 14:10:35
下一篇 2025年3月3日 04:26:44

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论