作业1
#include<stdio.h> int main() { int a,b,c,d; double ave,sum; printf("请输入4个整数:\n"); scanf("%d%d%d%d",&a,&b,&c,&d); sum=a+b+c+d; ave=sum/4; printf("ave=%.1f\n",ave); printf("sum=%.0f\n",sum); return 0; }
测试:
作业2
#include<stdio.h> #include<math.h> int main() { double x,sum; printf("Enter x:"); scanf("%lf",&x); if(x>0&&x<=50) { printf("sum=%.2f\n",sum=0.53*x); } else if(x>50) { printf("sum=%.2f\n",sum=50*0.53+(x-50)*0.58); } else { printf("无效!"); } return 0; }
测试:
作业3
#include<stdio.h> int main() { int i,a,b,sum1,sum2,denominator; double item,sum; printf("Enter b:"); scanf("%d",&b); printf("Enter a:"); scanf("%d",&a); sum=sum1=sum2=0; for(i=b;i<=a;i++) { sum1=sum1+b*b; } for(i=b;i<=a;i++) { item=1.0/denominator; sum2=sum2+item; denominator=denominator+1; } sum=sum1+sum2; printf("Sum of number from %d to %d is %.6lf\n",b,a,sum); return 0; }
测试:
作业4
#include<stdio.h> int main() { int denominator=1,flag=1,i,n; double item,sum=0; printf("Enter n:"); scanf("%d",&n); for(i=1;i<=n;i++) { item=flag*i*1.0/denominator; sum=sum+item; flag=-flag; denominator=denominator+2; } printf("sum=%f\n",sum); return 0; }
测试:
作业5
#include<stdio.h> #include<math.h> int main () { int i,n; double y,sum=0; printf ("Enter n:"); scanf("%d",&n); for(i=1;i<=n;i++) { y=sqrt(i); sum=sum+y; } printf("sum=%.2lf\n",sum); return 0; }
测试:
作业6
#include<stdio.h> double fact(int n); int main() { int i,n; double sum=0; printf("Enter n:"); scanf("%d",&n); for(i=1;i<=n;i++) { sum=sum+fact(i); } printf("sum=%.0lf\n",sum); return 0; } double fact(int n) { int i; double product=1; for(i=1;i<=n;i++) { product=product*i; } return product; }
测试: