下面是一個使用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;
}
通過輸入等差數列的首項、公差和項數,該程序將計算出等差數列的和并輸出結果。