2.2  #include<stdio.h>
main()
{
float x=2.5,y=2.5,z=2.5;
printf("x=%f\n",x);
printf("y=%f\n",y);
printf("z=%f\n",z);
}
3.1(1)
#include<stdio.h>
main()
{
int a=12,b=3;
float x=18.5,y=4.6;
printf("%f\n",(float)(a*b)/2);
printf("%d\n",(int)x%(int)y);
}
3.1(2)
#include<stdio.h>
main()
{
int x=32,y=81,p,q;
p=x++;
q=--y;
printf("%d %d\n",p,q);  printf("%d %d\n",x,y); }
3.2
#include<stdio.h>
main()
{
int x,b0,b1,b2,s;
printf("Inputx:");
scanf("%d",&x);
b2=x/100;
b1=(x-b2*100)/10;//或(x%100)/10;或x/10%10; b0=x%10;
printf("s=%d\n",s);
}
3.3
#include<stdio.h>
#include<math.h>
main()
{
float rate=0.0225;
float n,capital,deposit;
printf("Input n,capital:");
scanf("%f,%f",&n,&capital);
deposit=capital*pow(1+rate,n);
printf("deposit=%f\n",deposit);
}
3.4
#include<stdio.h>
#include<math.h>
main()
{
float a, b, c;
double x, y;
printf("Input a, b, c:");
scanf("%f %f %f", &a, &b, &c);
x=(-b+sqrt(b*b-4*a*c))/(2*a);
y=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("x=%f,y=%f\n",x,y);
}
习题4
4.1(1)
#include<stdio.h>
main()
{
char c1='a',c2='b',c3='c';
printf("a%cb%cc%c\n",c1,c2,c3);
}
4.1(2)
#include<stdio.h>
main()
{
int a=12,b=15;
printf("a=%d%%,b=%d%%\n",a,b);
}
#include<stdio.h>
main()
{
int a,b;
scanf("%2d%*2s%2d",&a,&b);
printf("%d,%d\n",a,b);
}
4.2
#include<stdio.h>
main()
{
long a,b;
float x,y;
scanf("%d,%d\n",&a,&b);
scanf("%f,%f\n",&x,&y);
printf("a=%d,b=%d\n",a,b);
printf("x=%f,b=%f\n",x,y);
}
5.1
#include<stdio.h>
main()
{
float a;
printf("Input a:");
scanf("%f",&a);
if(a>=0)
{
a=a;
printf("a=%f\n",a);
}
else
{
a=-a;
printf("a=%f\n",a);
}
}
5.2
#include<stdio.h>
main()
{
int a;
printf("Input a:");
scanf("%d", &a);
if(a%2==0)
printf("a是偶数");
}
else
{
printf("a是奇数");
}
}
5.3
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,s,area;
printf("Input a, b, c:");
scanf("%f %f %f", &a, &b, &c);
if(a+b>c&&a+c>b&&b+c>a)
{
s=(a+b+c)/2;
area=(float)sqrt(s*(s-a)*(s-b)*(s-c));
printf("area=%f\n",area);
}
else
{
printf("不是三角形");c语言程序设计教材答案
}
}
5.4
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,x,y;
printf("Inputa,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
if(a==0)
{
printf("该方程不是一元二次方程\n");
}
if(b*b-4*a*c>0)
{
x=(-b+sqrt(b*b-4*a*c))/(2*a);
y=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("x=%f,y=%f\n",x,y);
}
else if(b*b-4*a*c==0)
x=-b/(2*a);
y=-b/(2*a);
printf("x=%f,y=%f\n",x,y);
}
else
{
printf("该方程无实根\n");
}
}
5.5
#include<stdio.h>
main()
{
int year,flag;
printf("Input a year:");
scanf("%d",&year);
if(year%4==0&&year%400!=0||year%400==0)
{
flag=1;
}
else
{
flag=0;
}
if(flag==1)
{
printf("%d is a leap year!\n",year);
}
else
{
printf("%d is not a leap year!\n",year);
}
}
5.6
#include<stdio.h>
main()
{
int year,flag;
printf("Input a year:");
scanf("%d",&year);
flag=year%400==0||year%4==0&&year%100!=0?1:0;
if(flag==1&&flag!=0)
{
printf("%d is a leap year!\n",year);
}

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