编写一个 C 语言程序,使用数学公式和不使用数学公式来查找算术级数(A.P. 级数)的和。
算术级数是这样的一个序列,其中下一个元素是通过在前一项上加一个公差得到的。或者说,A.P. 级数是一个数字序列,其中任何两个连续数字之间的差始终是相同的。这称为公差。计算算术级数的数学公式
A.P. 级数和:Sn = n/2(2a + (n – 1) d)
A.P. 级数项:Tn = a + (n – 1) d
C 语言查找算术级数和的程序示例
此程序允许用户输入第一个值、级数中的元素总数以及公差。接下来,它将查找算术级数的和。在这里,我们使用 For 循环 来显示 A.P. 级数,这是可选的。
#include <stdio.h>
int main() {
int a, n, d, tn, i;
int sum = 0;
printf(" Please Enter First Number of an A.P Series: ");
scanf("%d", &a);
printf(" Please Enter the Total Numbers in this A.P Series: ");
scanf("%d", &n);
printf(" Please Enter the Common Difference: ");
scanf("%d", &d);
sum = (n * (2 * a + (n - 1) * d)) / 2;
tn = a + (n - 1) * d;
printf("\n The Sum of Series A.P. : ");
for(i = a; i <= tn; i = i + d)
{
if(i != tn)
printf("%d + ", i);
else
printf("%d = %d", i, sum);
}
printf("\n");
return 0;
}
Please Enter First Number of an A.P Series: 1
Please Enter the Total Numbers in this A.P Series: 4
Please Enter the Common Difference: 5
The Sum of Series A.P. : 1 + 6 + 11 + 16 = 34
使用 While 循环计算算术级数和的 C 语言程序
这个 C 语言程序 与上面的相同。但是,我们使用 While 循环来显示可选的 A.P. 级数。
#include <stdio.h>
int main() {
int a, n, d, tn, i;
int sum = 0;
printf(" Please Enter First Number of an A.P Series: ");
scanf("%d", &a);
printf(" Please Enter the Total Numbers in this A.P Series: ");
scanf("%d", &n);
printf(" Please Enter the Common Difference: ");
scanf("%d", &d);
sum = (n * (2 * a + (n - 1) * d)) / 2;
tn = a + (n - 1) * d;
i = a;
printf("\n The Sum of Series A.P. : ");
while(i <= tn)
{
if(i != tn)
printf("%d + ", i);
else
printf("%d = %d", i, sum);
i = i + d;
}
printf("\n");
return 0;
}
Please Enter First Number of an A.P Series: 2
Please Enter the Total Numbers in this A.P Series: 6
Please Enter the Common Difference: 3
The Sum of Series A.P. : 2 + 5 + 8 + 11 + 14 + 17 = 57
不使用数学公式的算术级数和
在此程序中,我们不使用任何 C 编程 数学公式。
#include <stdio.h>
int main() {
int a, n, d, value, i;
int sum = 0;
printf(" Please Enter First Number of an A.P Series: ");
scanf("%d", &a);
printf(" Please Enter the Total Numbers in this A.P Series: ");
scanf("%d", &n);
printf(" Please Enter the Common Difference: ");
scanf("%d", &d);
value = a;
printf("\n A.P. Series : ");
for(i = 0; i < n; i++)
{
printf("%d + ", value);
sum = sum + value;
value = value + d;
}
printf("\n The Sum of A.P Series until %d = %d\n", n, sum);
return 0;
}
Please Enter First Number of an A.P Series: 2
Please Enter the Total Numbers in this A.P Series: 6
Please Enter the Common Difference: 7
A.P. Series : 2 + 9 + 16 + 23 + 30 + 37 +
The Sum of A.P Series until 6 = 117
使用函数的算术级数和
这个 程序 与上面的相同。但是,我们使用 函数 分离了逻辑。
#include <stdio.h>
int sumofAP(int a, int n, int d)
{
int sum = (n * (2 * a + (n - 1) * d)) / 2;
return sum;
}
int main() {
int a, n, d;
int sum = 0;
printf(" Please Enter First Number of an A.P Series: ");
scanf("%d", &a);
printf(" Please Enter the Total Numbers in this A.P Series: ");
scanf("%d", &n);
printf(" Please Enter the Common Difference: ");
scanf("%d", &d);
sum = sumofAP(a, n, d);
printf("\n The Sum of Arithmetic Progression Series is = %d\n", sum);
return 0;
}
