《C语言程序的设计苏小红版答案.docx》由会员分享,可在线阅读,更多相关《C语言程序的设计苏小红版答案.docx(33页珍藏版)》请在优知文库上搜索。
1、2. 2#includemain()Ifloatx=2.5,y=2.5,z=2.5;printf(x=%fn,x);printf(y=%fn,y);printf(z=%fn,z);)3. 1(1)#includemain()Iinta=12,b=3;floatx=18.5,y=4.6;printf(%dn,(float)(a*b)2);printf(%dn”,(int)x%(int)y);)3. 2#includemain()Iintx,b,b1,b2,s;printf(,Inputx:);scanf(%d,x);b2=x100;b1=(-b2*100)10;b0=x%10;s=b0*100+
2、b1*10+b2;printf(s=dn,s);)3. 3#include#incIudemain()Ifloatrate=0.0225;floatn,capital,deposit;printf(Inputn,capitaI:);scanf(%f,%f,&n,&capital);deposit=capitaI*pow(1+rate,n);printf(deposit=%fn,deposit);J3. 4include#incIudemain()floata,b,c;doubIex,y;printf(Inputa,b,c:;scanf(%f,%f,%f,&a,&b,&c);x=(-b+sqrt
3、(b*b-4*a*c)/(2*a);y=(-b-sqrt(b*b-4*a*c)/(2*a);printf(x=%f,y=%fn,y);14. 1(1)#includemain()(chard=,a,c2=,b,c3=c;printf(,a%cb%cc%cn,d,c2,c3);)4.1 (2)#includemain()(inta=12,b=15;printf(,a=%d%,b=%d%n,a,b);)4.2 (3)#includemain()(inta,b;scanf(%2d%*2s%2d,&a,&b);printf(%d,%dn,a,b);)4.3 #includemain()(longa,b
4、;floatx,y;scanf(%d,%dn,&a,&b);scanf(%f,%fn,&x,&y);printf(a=%d,b=%dn,a,b);printf(x=f,b=%fn,x,y);J5. 1#includemain()(floata;printf(Innputa:,);scanf;if(a=0)a=a;printf(a=%fn,a);)else(a=-a;printf(a=%fn,a);)5. 2#includemain()Iinta;printf(Inputa:);scanf(%d,a);if(a%2=0)(Printf(a是偶数);)else(printf(a是奇数);)5. 3
5、#include#incIudemain()Ifloata,b,c,s,area;printf(Inputa,b,c:);scanf(n%f,%f,%f,&a,&b,&c);if(a+bc&a+cb&b+ca)(s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c);printf(area=%fn,area);)else(Printf(不是三角形);)5.4#include#incIudemain()floata,b,c,y;printf(,Inputa,b,c:;scanf(%f,%f,f,&a,&b,&c);if(a=0)(Printf(该方程不是
6、一元二次方程n);)if(b*b-4*a*cO)(x=(-b+sqrt(b*b-4*a*c)/(2*a);y=(-b-sqrt(b*b-4*a*c)/(2*a);Printf(x=%f,y=%fn,x,y);)elseif(b*b-4*a*c=0)(=-b(2*a);y=-b(2*a);printf(=%f,y=%fn,x,y);)else(Printf(该方程无实根n);)5.5#includemain()(intyear,flag;printf(Inputayear:);scanf(%d,&year);if(year%4=0&year%400!=0year%400=0)(fag=1;)el
7、se(fag=0;)if(fIag=Dprintf(,%disaleapyear!n,year);else(printf(%disnotaleapyear!n,year);)5.6#includemain()Iintyear,flag;printf(Inputayear:);scanf(,%d,&year);fIag=year%400=0year%4=0&year%100!=0?1:0;if(f|ag=1&flag!=0)(printf(%disaleapyear!n,year);)else(printf(%disnotaleapyear!n,year);)5.7#includemain()I
8、charch;printf(Inputch:);scanf(%c,&ch);if(ch=,a,aach=,A,Sch=,Z)(ch=getchar();ch=ch+32;printf(%c,%dn,ch,ch);)elseprintf(%c,ch);5. 8#includemain()charch;printf(Inputch:,);scanf(%c,&ch);if(ch=48&ch=65&ch=97&ch=122)(Printf(ch是小写字母n);)elseif(ch=32)(printf(,ch是空格n);printf(ch是其他字符n);6. 9#includemain()(intsc
9、ore,grade;printf(Inputscore:);scanf(%d,&score);grade=score10;if(score100)Iprintf(Inputerrorn);if(score=90&score=80&score=70&score=60&score=0&score60)(printf(%dEn,score);)J7. 10#includemain()(intyear,month;printf(Inputyear,month:,);scanf(%d,%d,&year,&month);if(month121month=0)(printf(errormonthn,);)e
10、Ise(switch(year,month)(case12:case10:case8:case7:case5:case3:case1:printf(31天n);break;case11:case9:case6:case4:printf(30天n);break;case2:if(year%4=0&year!=0year%4OO=O)(printf(,29天n);)else(printf(,28天n);)break;default:printf(,Inputerrorn);)1J8. 1(1)includemain()(inti,j,k;charspace=,;for(i=1;i=4;i+)(fo
11、r(j=1;j=i;j+)(printf(%c,space);)for(k=1;k=6;k+)(printf(*);)printf(,n);)8.1 (2)#includemain()Iintk=4,n;for(n=0;nk;n+)(if(n%2=0)continue;k-;)printf(k=%dn,n=%dn,k,n);6. 1(3)#includemain()I1 ntk=4,n;for(n=0;nk;n+)(1 f(n%2=0)break;k一;)printf(k=%d,n=%dn,k,n);J6.2(1)#includemain()(inti,sum=0;for(i=1;i=101;i+)(sum=sum+i;)printf(sum=%dn,sum);)6.2(2)#includemain()Ilongi;longterm,sum=0;for(i=1;i=101;i=i+2)Iterm=i*(i+1)*(i+2);sum=sum+term;)printf(sum=%ldn,sum);)6.2(4)#include