0

0

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

王林

王林

发布时间:2023-09-11 21:21:03

|

1548人浏览过

|

来源于tutorialspoint

转载

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.

matlab基础知识简介 中文WORD版
matlab基础知识简介 中文WORD版

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

Example

#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

相关专题

更多
if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

711

2023.08.22

string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

312

2023.08.02

int占多少字节
int占多少字节

int占4个字节,意味着一个int变量可以存储范围在-2,147,483,648到2,147,483,647之间的整数值,在某些情况下也可能是2个字节或8个字节,int是一种常用的数据类型,用于表示整数,需要根据具体情况选择合适的数据类型,以确保程序的正确性和性能。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

521

2024.08.29

c++怎么把double转成int
c++怎么把double转成int

本专题整合了 c++ double相关教程,阅读专题下面的文章了解更多详细内容。

48

2025.08.29

C++中int的含义
C++中int的含义

本专题整合了C++中int相关内容,阅读专题下面的文章了解更多详细内容。

190

2025.08.29

页面置换算法
页面置换算法

页面置换算法是操作系统中用来决定在内存中哪些页面应该被换出以便为新的页面提供空间的算法。本专题为大家提供页面置换算法的相关文章,大家可以免费体验。

387

2023.08.14

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

2

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

6

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

16

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 4.1万人学习

Git 教程
Git 教程

共21课时 | 2.3万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.0万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号