如何使用多种语言查找算术系列之和(解决方案)

148 阅读3分钟

算术序列是一个序列,其中每个项与前面的项相差一个常量。知道如何找到这些可以帮助你建立你的编程技能,无论你使用哪种语言。

在这篇文章中,你将学习如何使用Python、C++、JavaScript和C语言寻找算术级数之和。

什么是算术级数?

一个有限的算术序列的项之和被称为算术序列。算术序列表示如下。

a, a+d, a+2d, a+3d, a+4d, ...

其中。

a = First termd = Common difference

问题陈述

你得到了算术数列的首项、公差和项的数目。你需要找出该算术数列的和。例子。设首项=1,公差=2,无项=5。算术系列。1+3+5+7+9 算术数列之和:25 因此,输出为25。

寻找算术数列之和的迭代方法

首先,我们来看看迭代法。你可以在下面的主要编程语言中找到用这种方法求和的方法。

用迭代法求算术数列之和的C++程序

下面是用迭代法求算术数列之和的C++程序。

// C++ program to find the sum of arithmetic series#include <iostream>using namespace std;// Function to find the sum of arithmetic seriesint sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms){ int result = 0; for (int i=0; i<noOfTerms; i++) { result = result + firstTerm; firstTerm = firstTerm + commonDifference; } return result;}int main(){ int firstTerm = 1; int commonDifference = 2; int noOfTerms = 5; cout << "First Term: " << firstTerm << endl; cout << "Common Difference: " << commonDifference << endl; cout << "Number of Terms: " << noOfTerms << endl; cout << "Sum of the arithmetic series: " << sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) << endl;return 0;}

输出。

First Term: 1Common Difference: 2Number of Terms: 5Sum of the arithmetic series: 25

用迭代法求算术数列之和的Python程序

下面是用迭代法求算术数列之和的Python程序。

# Python program to find the sum of arithmetic series# Function to find the sum of arithmetic seriesdef sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms):    result = 0    for i in range(noOfTerms):        result = result + firstTerm        firstTerm = firstTerm + commonDifference    return resultfirstTerm = 1commonDifference = 2noOfTerms = 5print("First Term:", firstTerm)print("Common Difference:", commonDifference)print("Number of Terms:", noOfTerms)print("Sum of the arithmetic series:", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms))

输出。

First Term: 1Common Difference: 2Number of Terms: 5Sum of the arithmetic series: 25

相关的。如何在Python中使用For循环

用迭代法求算术数列之和的JavaScript程序

下面是用迭代法求算术数列之和的JavaScript程序。

// JavaScript program to find the sum of arithmetic series// Function to find the sum of arithmetic seriesfunction sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) { var result = 0; for (let i=0; i<noOfTerms; i++) { result = result + firstTerm; firstTerm = firstTerm + commonDifference; } return result;}var firstTerm = 1;var commonDifference = 2;var noOfTerms = 5;document.write("First Term: " + firstTerm + "<br>");document.write("Common Difference: " + commonDifference + "<br>");document.write("Number of Terms: " + noOfTerms + "<br>");document.write("Sum of the arithmetic series: " + sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));

输出。

First Term: 1Common Difference: 2Number of Terms: 5Sum of the arithmetic series: 25

相关的。如何找到数组中所有元素的乘积

用迭代法求算术数列之和的C语言程序

下面是用迭代法求算术数列之和的C语言程序。

// C program to find the sum of arithmetic series#include <stdio.h>// Function to find the sum of arithmetic seriesint sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms){ int result = 0; for (int i=0; i<noOfTerms; i++) { result = result + firstTerm; firstTerm = firstTerm + commonDifference; } return result;}int main(){ int firstTerm = 1; int commonDifference = 2; int noOfTerms = 5; printf("First Term: %d \⁠n", firstTerm); printf("Common Difference: %d \⁠n", commonDifference); printf("Number of Terms: %d \⁠n", noOfTerms); printf("Sum of the arithmetic series: %d \⁠n", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));return 0;}

输出。

First Term: 1Common Difference: 2Number of Terms: 5Sum of the arithmetic series: 25

用公式求算术数列之和的有效方法

你可以用下面的公式来求算术级数之和。

Sum of arithmetic series = ((n / 2) * (2 * a + (n - 1) * d))

其中。

a = First termd = Common differencen = No. of terms

用公式求算术数列之和的C++程序

下面是用公式求算术数列之和的C++程序。

// C++ program to find the sum of arithmetic series#include <iostream>using namespace std;// Function to find the sum of arithmetic seriesint sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms){ return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference);}int main(){ int firstTerm = 1; int commonDifference = 2; int noOfTerms = 5; cout << "First Term: " << firstTerm << endl; cout << "Common Difference: " << commonDifference << endl; cout << "Number of Terms: " << noOfTerms << endl; cout << "Sum of the arithmetic series: " << sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) << endl;return 0;}

输出。

First Term: 1Common Difference: 2Number of Terms: 5Sum of the arithmetic series: 25

使用公式求算术数列之和的Python程序

下面是利用公式求算术数列之和的Python程序。

# Python program to find the sum of arithmetic series# Function to find the sum of arithmetic seriesdef sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms):    return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference)firstTerm = 1commonDifference = 2noOfTerms = 5print("First Term:", firstTerm)print("Common Difference:", commonDifference)print("Number of Terms:", noOfTerms)print("Sum of the arithmetic series:", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms))

输出。

First Term: 1Common Difference: 2Number of Terms: 5Sum of the arithmetic series: 25

相关的。如何在C、C++、Python和JavaScript中使用递归实现线性搜索

使用公式查找算术数列之和的JavaScript程序

下面是用公式求算术数列之和的JavaScript程序。

// JavaScript program to find the sum of arithmetic series// Function to find the sum of arithmetic seriesfunction sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) { return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference);}var firstTerm = 1;var commonDifference = 2;var noOfTerms = 5;document.write("First Term: " + firstTerm + "<br>");document.write("Common Difference: " + commonDifference + "<br>");document.write("Number of Terms: " + noOfTerms + "<br>");document.write("Sum of the arithmetic series: " + sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));

输出。

First Term: 1Common Difference: 2Number of Terms: 5Sum of the arithmetic series: 25

用公式求算术数列之和的C语言程序

下面是用公式求算术数列之和的C语言程序。

// C program to find the sum of arithmetic series#include <stdio.h>// Function to find the sum of arithmetic seriesint sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms){ return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference);}int main(){ int firstTerm = 1; int commonDifference = 2; int noOfTerms = 5; printf("First Term: %d \⁠n", firstTerm); printf("Common Difference: %d \⁠n", commonDifference); printf("Number of Terms: %d \⁠n", noOfTerms); printf("Sum of the arithmetic series: %d \⁠n", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));return 0;}

输出。

First Term: 1Common Difference: 2Number of Terms: 5Sum of the arithmetic series: 25

用不同的编程语言查找算术级数是很容易的

现在你已经阅读了这篇文章,你知道如何用每种主要的编程语言寻找算术级数。

C++是 "面包和黄油 "编程语言之一。它被用来开发各种软件,如数据库、操作系统、编译器、网络浏览器等。如果你想学习C++,你应该看看一些最好的网站,如Udemy、edX、LearnCpp等等。