C语言程序设计部分实验参考答案
实验2
#include<stdio.h>
int main(){//2-1-2
int c,f=150;
c=f*5/9-32*5/9;
printf("fahr=%d, celsius=%d\n",f, c);
return 0;
}
#include<stdio.h>
int main(){//2-1-3
int c=26,f;
f=c*9/5+32;
printf("celsius=%d, fahr=%d\n",c, f);
return 0;
}
#include<stdio.h>
int main(){//2-1-4
int math=87,eng=72,comp=93;
printf("math=%d, eng=%d, comp=%d, average=%d\n",math, eng, comp, (math+eng+comp)/3);
return 0;
}
#include<stdio.h>
int main(){//2-1/5
int n=152,a,b,c;
c=n%10;
b=(n/10)%10;
a=n/100;
printf("整数%d 的个位数字是%d, 十位数字是%d, 百位数字是%d\n",n, c, b, a);
return 0;
}
#include<stdio.h>
int main(){//2-1-6
int x=3,y;
y=x*x;
printf("%d=%d*%d\n",y,x,x);
printf("%d*%d=%d\n",x,x,y);
return 0;
}
#include<stdio.h>
int main(){//2-2-1
double x, y;
printf("Enter x:");
scanf_s("%lf",&x);
if(x!=0){
y=1/x;
}else{
y=0;
}
printf("f(%.2f)=%.1f\n",x,y);
return 0;
}
#include<stdio.h>
int main(){//2-2-2
int celsius, fahr;
fahr=100;
celsius=5*(fahr-32)/9;
printf("fahr=%d, celsius=%d\n",fahr,celsius);
return 0;
}
#include<stdio.h>
int main(){//2-2-3
double money, rate,x=1.0;
int i,year;
printf("Enter money, year and rate:");
scanf("%lf %d %lf",&money,&year,&rate);
for(i=1;i<=year;i++)
x=x*(1+rate);
printf("intrest=%.2f\n",money*x-money);
return 0;
}
#include<stdio.h>
#include<math.h>
int main(){//2-2-4
double x, y;
printf("Enter x");
scanf_s("%lf",&x);
if(x<0.0)
y=pow(x+1,2)+2*x+1/x;
else
y=sqrt(x);
printf("f(%.2f)=%.2f\n",x,y);
return 0;
}
#include<stdio.h>
#include<math.h>
int main(){//2-2-5
double x, y;
printf("Enter x:");
scanf_s("%lf",&x);
if(x==10)
y=1/x;
else
y=x;
printf("f(%.2f)=%.1f\n",x,y);
return 0;
}
c语言程序设计教材答案#include<stdio.h>
int main(){//2-2-6
int x, y;
printf("Enter num1:");
scanf_s("%d",&x);
printf("Enter num2:");
scanf_s("%d",&y);
printf("%d+%d=%d\n",x,y,x+y);
printf("%d-%d=%d\n",x,y,x-y);
printf("%d*%d=%d\n",x,y,x*y);
printf("%d/%d=%d\n",x,y,x/y);
printf("%d%%d=%d\n",x,y,x%y);
return 0;
}
#include<stdio.h>
int main(){//2-3-1
int i,sum=0;
for(i=1;i<=100;i++){
sum=sum+i;
}
printf("sum=%d\n",sum);
return 0;
}
#include<stdio.h>
int main(){//2-3-2
int i,m,s=0;
printf("Enter m:");
scanf("%d",&m);
for(i=m;i<=100;i++)
s=s+i;
printf("sum=%d\n",s);
return 0;
}
#include<stdio.h>
int main(){//2-3-3
int i,m,n;
double s=0.0;
printf("Enter m:");
scanf("%d",&m);
printf("Enter n:");
scanf("%d",&n);
for(i=m;i<=n;i++)
s=s+1.0/i;
printf("sum=%f\n",s);
return 0;
}
#include<stdio.h>
int main(){//2-3-4
int i,n;
double s=0.0;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
s=s+1.0/(2*i-1);
printf("sum=%f\n",s);
return 0;
}
#include<stdio.h>
int main(){//2-3-5
int i,n;
double s=1.0,t=1.0,sign=1.0;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<n;i++){
sign=-sign;
t=(t+3);
s=s+1/t*sign;
}
printf("sum=%.3f\n",s);
return 0;
}
#include<stdio.h>
int main(){//2-3-6
int  low, uper;
double fahr,celsius;
printf("Enter low:");
scanf_s("%d",&low);
printf("Enter uper:");
scanf_s("%d",&uper);
printf("fahr celsius\n");
for(fahr=low;fahr<=uper;fahr+=2){
celsius=5.0/9.0*(fahr-32.0);
printf("%3.0f %6.1f\n",fahr,celsius);
}
return 0;
}
#include<stdio.h>
int main(){//2-3-7
int m, n, i;
double p,sum=0.0;
printf("Enter m:");
scanf("%d",&m);
printf("Enter n:");
scanf("%d",&n);
p=1.0;
for(i=1;i<=m;i++)
p=p*i;
sum=sum+p;
p=1.0;
for(i=1;i<=n;i++)
p=p*i;
sum=sum+p;
printf("sum=%.0f\n",sum);
return 0;
}
#include<stdio.h>
int main(){//2-3-8
int x, n,i;
double p=1.0;
printf("Enter x:");
scanf("%d",&x);
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
p=p*x;
printf("%d的%d次方是%.0f\n",x, n,p);
return 0;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。