几何级数系列第N项的C程序

给定第一个术语“ a”,给定公共比率“ r”,而系列中的术语数为“ n”。任务是找到系列的第n个术语。

因此,在讨论如何为该问题编写程序之前,我们应该知道什么是几何级数。

数学中的几何级数或几何序列是通过将前一个项与固定比例项的公共比率相乘来找到第一个项之后的每个项。

像2、4、8、16、32 ..一样,是第一项为2且公比为2的几何级数。如果n = 4,则输出为16。

因此,我们可以说第n个项的几何级数将类似于-

GP1 = a1

GP2 = a1 * r^(2-1)

GP3 = a1 * r^(3-1)

. . .

GPn = a1 * r^(n-1)

因此公式将为GP = a * r ^(n-1)。

示例

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)

Stop

示例

#include <stdio.h>

#include <math.h>

//函数返回GP的n项

int Nth_of_GP(int a, int r, int n) {

   //第N个词是

   return( a * (int)(pow(r, n - 1)) );

}

//主块

int main() {

   //初始编号

   int a = 1;

   //普通比率

   int r = 2;

   //第N个名词

   int n = 8;

   printf("The %dth term of the series is: %d\n",n, Nth_of_GP(a, r, n) );

   return 0;

}

输出结果

The 8th term of the series is: 128

以上是 几何级数系列第N项的C程序 的全部内容, 来源链接: utcz.com/z/338227.html

回到顶部