c语言上机作业及答案

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
printf("1!+2!+...+10! = %ld \n", sum);
}
#include <stdio.h>
main()
{
long term ,sum = 0;
int i, j;
for (i = 1; i <= 10; i++)
{
term = 1;
for (j = 1; j <= i; j++)
4.11编程计算1×2×3+3×4×5+…+99×100×101的值。
#include <stdio.h>
main()
{
long i ;
long term, sum = 0;
for (i = 1; i <= 99; i = i + 2)
{
term = i * (i + 1) * (i + 2);
sum = sum + term;
result = result * term;
}
printf("result = %f\n", 2*result);
}
4.15利用泰勒级数e …+ 计算e的近似值,当最后一项的绝对值小于105时认为达到了精度要求。要求统计总共累加了多少项。
#include <math.h>
#include <stdio.h>
{
term = term * j;
}
sum = sum + term;
}
printf("1!+2!+…+10! = %ld \n", sum);
}
4.13编程计算a+aa+aaa+…+aa…a(n个a)的值,n和a的值由键盘输入。
#include <stdio.h>
main()
{
long term = 0,sum = 0;
result = result * term;
}
printf("result = %f\n", 2*result);
}
#include <stdio.h>
main()
{
double term, result = 1;
int n;
for (n = 1; n <= 50; n++)
{
term = (double)(2*n*2*n)/((2*n-1)*(2*n+1)); /*计算累乘项*/
while (fabs(term) >= 1e-4)/*判断末项大小*/
{
term = sign / n;/*求出累加项*/
sum = sum + term;/*累加*/
sign = -sign;/*改变项的符号*/
n++;/*分母加1*/
}பைடு நூலகம்
printf("sum = %f\n", sum);
}
4.17利用泰勒级数sin(x)≈ 计算sin(x)的值。要求最后一项的绝对值小于10-5,并统计出此时累加了多少项。
int a , i, n;
printf("Input a,n: ");
scanf("%d,%d", &a, &n);/*输入a,n的值*/
for (i = 1; i <= n; i++)
{
term = term * 10 + a;/*求出累加项*/
sum = sum + term;/*进行累加*/
}
}
printf("sum=%ld\n",sum);
}
4.14利用 = ×…的前100项之积计算的值。
#include <stdio.h>
main()
{
double term, result = 1;/*累乘项初值应为1*/
int n;
for (n = 2; n <= 100; n = n + 2)
{
term = (double)( n * n)/(( n - 1) * ( n + 1));/*计算累乘项*/
}
printf("sum=%ld",sum);
}
4.12编程计算1!+2!+3!+4!+…+10!的值。
#include <stdio.h>
main()
{
long term = 1,sum = 0;
int i;
for (i = 1; i <= 10; i++)
{
term = term * i;
sum = sum + term;
term = x;/*赋初值*/
do
{
term = -term * x * x / ((n + 1) * (n + 2));
sum = sum + term;/*累加*/
n = n + 2;
count++;
}while (fabs(term) >= 1e-5);
printf("sin(x) = %f, count = %d\n", sum, count);
#include <math.h>
#include <stdio.h>
main()
{
int n = 1,count = 1;
float x;
double sum , term;/*因为位数多,所以定义为双精度*/
printf("Input x: ");
scanf("%f", &x);
sum = x;
}
printf("e = %f, count = %d\n", e, count);
}
4.16计算 ,直到最后一项的绝对值小于10-4为止。
#include <stdio.h>
#include <math.h>
main()
{
int n = 1;
float term = 1.0, sign = 1,sum = 0;
main()
{
intn = 1, count =1;
doublee = 1.0, term = 1.0;
longfac = 1;
for (n=1; fabs(term) >= 1e-5; n++)
{
fac = fac * n;
term = 1.0 / fac;
e = e + term;
count++;
相关文档
最新文档