C语言程序设计练习题目1、求两数之和#include“stdio.h”voidmain(){inta,b,sum;/*定义变量*/scanf(“%d%d”,&a,&b);/*给变量赋值*/sum=a+b;/*求a与b的和*/printf(“sumis%d\n”,sum);/*输出结果sumis579*/}2、求两数中的大数#include“stdio.h”/*包含头文件*/voidmain()/*主函数*/{inta,b,c;/*定义变量*/scanf(“%d%d”,&a,&b);/*输入变量a,b的值*/c=max(a,b);/*调用函数,将得到的值赋给c*/printf(“max=%d\n”,c);/*输出c的值*/}intmax(intx,inty)/*子函数,定义max函数,函数值为整型,x、y为形参,整型*/{intz;/*函数中要用到的内部变量z,也要加以定义。*/if(xy)z=x;/*比较x、y的大小,如果x大于y,则执行z=x*/elsez=y;/*否则执行z=y;*/return(z);/*将z的值返回,通过max带回调用处*/}3、输入一个小写字母,打印其大写字母及其前导字母与后续字母分析:小写字母的ASCII码比对应的大写字母大32(例A、a的ASCII码分别是65、97)#include“stdio.h”voidmain(){charc;printf(“请输入任意一个小写字母:”);c=getchar();printf(“%c,%c,%c\n”,c,c-1,c+1);printf(“%c,%c,%c\n”,c-32,c-33,c-31);}4、求方程ax2+bx+c=0的根,设b2-4ac0。(其中a,b,c的值由键盘输入)#include“stdio.h”/*头文件中包含输入输出函数printf,scanf*/#include“math.h”voidmain(){floata,b,c,x1,x2,dt,p,q;printf(“输入a,b,c:”);scanf(“%f%f%f”,&a,&b,&c);dt=sqrt(b*b–4*a*c);p=-b/(2*a);q=dt/(2*a);x1=p+q;x2=p-q;printf(“方程%6.2fX*X+%6.2fX+%6.2f=0的根为:\n”,a,b,c);printf(“x1=%f\tx2=%f\n”,x1,x2);}5、鸡兔同笼。已知鸡兔总头数为h,总脚数为f,求鸡兔各多少只?#includestdio.hvoidmain(){intx,y,h,f;printf(“输入h,f:”);scanf(“%d%d”,&h,&f);x=2*h–f/2;y=f/2–h;printf(“头=%d,脚=%d\n”,h,f);printf(“鸡=%d,兔=%d\n”,x,y);}6、输入一个整数a,判断它是偶数还是奇数?#includestdio.hvoidmain(){intaprintf(“\tInputanumber:”);scanf(“%d”,&a);if(a%2==0)printf(“\n\t%diseven\n”,a);elseprintf(“\n\t%disodd\n”,a);}7、任给a,b,c三个数,按从大到小的顺序输出#includestdio.hvoidmain(){inta,b,c,t;printf(“inputa,b,c:”);scanf(“%d,%d,%d”,&a,&b,&c);if(ab){t=a;a=b;b=t;}if(ac){t=a;a=c;c=t;}if(bc){t=b;b=c;c=t;}printf(“%d%d%d\n”,a,b,c);}8、评价学生的成绩score≥90优秀score≥80良好score≥70中score≥60及格score60不及格#includestdio.hvoidmain(){intscore;printf(“inputascore:”);scanf(“%d”,&score);if(score=90)printf(“优秀\n”);elseif(score=80)printf(“良好\n”);elseif(score=70)printf(“中\n”);elseif(score=60)printf(“及格\n”);elseprintf(“不及格\n”);}9、设平面上一点M,其坐标为(x,y),若M落在圆心在坐标原点的单位圆上,则置key=1,落在圆外置key=2,落在圆内置key=0。10、计算函数:)3(log)33(9)3(1102xxxxxxy#includemath.hvoidmain(){floatx,y;printf(“\tinputx:”);scanf(“%f”,&x);if(x-3.0)y=x-1.0;else{if(x=-3.0&&x=3.0)y=sqrt(9.0-x*x)elsey=log10(x);}printf(“\tx=%.1f\ty=%.1f\n”,x,y);}11、判定某年是否闰年。闰年:年份year能被4整除,但不能被100整除;或能被400整除yrM(x,y)01x分析:1)求点M到原点的距离r2)比较r和单位圆半径的大小,从而得到相应的key值。输入x、y22yxrr1?是否key=2r=1?是否key=1key=0输出key#includestdio.h#includemath.hvoidmain(){floatx,y,r;intkey;printf(“inputx,y:”);scanf(“%f,%f”,&x,&y);r=sqrt(x*x+y*y);if(r1)key=2elseif(r==1)key=1;elsekey=0;printf(“(%.1f,%.1f):%d\n”,x,y,key);}执行结果:inputx,y:1.5,0.3(1.5,0.3):2#includestdio.hvoidmain(){intyear,leap;printf(“Whichyear?”);scanf(“%d”,&year);if(year%400==0)leap=1;else{if(year%4==0){if(year%100==0)leap=0;elseleap=1;}elseleap=0;}if(leap==1)printf(“%d年是闰年\n”,year);elseprintf(“%d年不是闰年\n”,year);}12、任意输入一个字符,若是大写字母将其转换成小写字母。#includestdio.hvoidmain(){charch;ch=getchar();ch=(ch=’A’&&ch=‘Z’)?ch+32:‘*’;if(ch!=‘*’){putchar(ch);putchar(‘\n’);}}13、运输公司对用户计算运费。收费标准随距离s变化如下:s250km不折扣250≤s500km2%500≤s1000km5%1000≤s2000km8%2000≤s3000km10%3000≤s15%分析:计费是按照运输距离s和货物重量w进行的。设每公里每吨货物的基本运费为p,考虑折扣d时,总运费的计算公式为:f=p*w*s*(1-d)#includestdio.hvoidmain(){floats,f,p,w,d;printf(“输入单价、距离和货物重量:”);scanf(“%f,%f,%f”,&p,&s,&w);switch((int)s/250)/*强制类型转换*/{case0:d=0;break;case1:d=0.02;break;case2:case3:d=0.05;break;case4:case5:case6:case7:d=0.08;break;case8:case9:case10:case11:d=0.1;break;default:d=0.15;}f=p*s*w*(1-d);printf(“总运费%.2f\n”,f);}14、设计一个简单的计算器,完成两个数的加减乘除分析:输入二个数及运算符switch表达式:运算符常量:‘+’,‘-’,‘*’,‘/’#includestdio.hvoidmain(){floatx,y;charop;printf(“enterx,opration,y:“);scanf(“%f%c%f”,&x,&op,&y);/*输入数据时不能有空格*/switch(op);{case‘+’:printf(“%.2f%c%.2f=%.2f\n”,x,op,y,x+y);break;case‘-’:printf(“%.2f%c%.2f=%.2f\n”,x,op,y,x-y);break;case‘*’:printf(“%.2f%c%.2f=%.2f\n”,x,op,y,x*y);break;case‘/’:if(y==0.0)printf(“error\n”);elseprintf(“%.2f%c%.2f=%.2f\n”,x,op,y,x/y);break;}}15、输入三角形的三边长,求三角形面积。假设:三个边长a,b,c能构成三角形。#includestdio.h#includemath.hvoidmain(){floata,b,c,s,area;scanf(“%f,%f,%f,&a,&b,&c);s=1.0/2*(a+b+c);area=sqrt(s*(s-a)*(s-b)*(s-c));printf(“a=%7.2f,b=%7.2f,c=%7.2f,s=%7.2f\n”,a,b,c,s);printf(“area=%7.2f\n”,area);}16、从键盘输入一个大写字母,要求改用小写字母输出。#includestdio.hvoidmain(){charcl,c2;cl=getchar();printf(″%c,%d\n″,cl,cl);c2=cl+32;printf(″%c,%d\n″,c2,c2);}17、输入两个实数,按代数值由小到大的顺序输出这两个数。#includestdio.hvoidmain(){floata,b,t;scanf(″%f,%f″,&a,&b);if(ab){t=a;a=b;b=t;}printf(″%5.2f,%5.2f\n″,a,b);}18、有一个函数)0(1)0(0)0(1xxxy,编一程序,输入一个x值,输出y值。#includestdio.hvoidmain(){intx,y;scanf(%d,&x);if(x0)y=-1;elseif(x==0)y=0;elsey=1;printf(x=%d,y=%d\n,x,y);}19、求ax2+bx+c=0方程的解。#includestdio.h#includemath.hvoidmain(){floata,b,c,disc,x1,x2,realpart,imagpart;scanf(%f,%f,%f,&a,&b,&c);printf(theequation);if(fabs(a)=1e-6)printf(isnotaquadratic\\n);else{disc=b*b-4*a*c;if(fabs(disc)=1e-6)printf(hastwoequalroots:%8.4f\n,-b/(2*a));elseif(disc1e-6){x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);printf(″hasdistinctrealroots:%8.4fand%8.4f\n″,x1,x2);}else{realpart=-b/(2*a);imagpart=sqrt(-disc)/(2*a);printf(″hascomplexroots∶\n″);printf(″%8.4f+%8.4fi\n″,realpart,