c语言等差数列求和程序怎么实现

   2024-10-05 8470
核心提示:下面是一个使用C语言实现等差数列求和的程序示例:#include stdio.hint main() {int firstTerm, commonDifference, n, sum = 0;/

下面是一个使用C语言实现等差数列求和的程序示例:

#include <stdio.h>int main() {    int firstTerm, commonDifference, n, sum = 0;    // 输入等差数列的首项、公差和项数    printf("Enter the first term of the arithmetic sequence: ");    scanf("%d", &firstTerm);    printf("Enter the common difference of the arithmetic sequence: ");    scanf("%d", &commonDifference);    printf("Enter the number of terms in the arithmetic sequence: ");    scanf("%d", &n);    // 计算等差数列的和    sum = n * (2 * firstTerm + (n - 1) * commonDifference) / 2;    // 输出结果    printf("The sum of the arithmetic sequence is: %d\n", sum);    return 0;}

通过输入等差数列的首项、公差和项数,该程序将计算出等差数列的和并输出结果。

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号