您好,欢迎访问一九零五行业门户网

C程序计算等差数列的第N项

given ‘a’ the first term, ‘d’ the common difference 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 arithmetic progression.
arithmetic progression or arithmetic sequence is a sequence of number where the difference between the two consecutive terms is same.
like we have first term i.e a =5, difference 1 and nth term we want to find should be 3. so, the series would be: 5, 6, 7 so the output must be 7.
so, we can say that arithmetic progression for nth term will be like −
ap1 = a1ap2 = a1 + (2-1) * dap3 = a1 + (3-1) * d..apn = a1 + (n-1) *

so the formula will be ap = a + (n-1) * d.
exampleinput: a=2, d=1, n=5output: 6explanation: the series will be:2, 3, 4, 5, 6 nth term will be 6input: a=7, d=2, n=3output: 11
approach we will be using to solve the given problem −
take first term a, common difference d, and n the number of series.then calculate nth term by (a + (n - 1) * d)return the output obtained from the above calculation.algorithmstart step 1 -> in function int nth_ap(int a, int d, int n) return (a + (n - 1) * d) step 2 -> int main() declare and initialize the inputs a=2, d=1, n=5 print the result obtained from calling the function nth_ap(a,d,n)stop
example#include <stdio.h>int nth_ap(int a, int d, int n) { // using formula to find the // nth term t(n) = a(1) + (n-1)*d return (a + (n - 1) * d);}//main functionint main() { // starting number int a = 2; // common difference int d = 1; // n th term to be find int n = 5; printf("the %dth term of ap :%d
", n, nth_ap(a,d,n)); return 0;}
输出the 5th term of the series is: 6
以上就是c程序计算等差数列的第n项的详细内容。
其它类似信息

推荐信息