您好,欢迎访问三七文档
当前位置:首页 > 机械/制造/汽车 > 机械/模具设计 > C语言上机练习题及答案
1输入三个整数x,y,z,把这三个数由小到大输出。#includestdio.hvoidmain(){intx,y,z,a;scanf(%d,%d,%d,&x,&y,&z);if(xy){a=x;x=y;y=a;}if(xz){a=x;x=z;z=a;}if(yz){a=y;y=z;z=a;}printf(%d,%d,%d\n,x,y,z);}2输入圆的半径,输出圆的周长和面积。#includestdio.h#definePI3.1415926#defineS==PI*r*r#defineV(4.0/3)*PI*r*r*rvoidmain(){doubler;printf(pleaseinputr:);scanf(%lf,&r);printf(areais%.4lf\n,S);printf(volumeis%.4lf\n,V);}输入正方形的边长,输出正方形的周长和面积。#includestdio.hvoidmain(){floatc,zc,mj;printf(输入你的正方形);scanf(%f\n,&c);zc=4*c;mj=c*c;printf(周长%f,面积%f,边长%f,zc,mj,c);}3用格式输入函数输入3个字符,并用输出函数反向输出3个字符和他们的ASCII码。#includestdio.hintmain(){char-ch1,ch2,ch3;printf(pleaseinputthreecharacters:\n);scanf(%C%C%c,&ch1,&ch2,&ch3);printf(%c\n%d\n,ch3,ch3);printf(%c\n%d\n,ch2,ch2);printf(%c\n%d\n,ch1,ch1);}输入一个摄氏温度,要求输出华氏温度。公式为f=9/5*c+32。#includestdio.hvoidmain(){floatC,F;scanf(%f,&F);C=5.0/9*(F-32);printf(%8.2f,C);}4。比较两个数大小的题#includestdio.hvoidmain(){inta,b;scanf(%d,%d,&a,&b);if(ab)printf(%d\n,a);elseprintf(%d\n,b);}第二题:此题作废(千万不要用)#includestdio.hvoidmain(){chara,b,c;scanf(%c,%c,%c,&a,&b,&c);while((a97||a122)||(b97||b122)||(c97||c122));{printf(您输入非三个小写字母,请重新输入:\n);scanf(%c,%c,%c,&a,&b,&c);}printf(%c,%c,%c\n,a-32,b-32,c-32);}5输入任意10个数,求他们的平均值。#includestdio.hvoidmain(){Printf(请输入10个整数:);inta[10],k=0,sum=0;for(inti=0;i10;++i);{ia[i];if10=a[i]0++k;sum=sum+a[i];}floatave=sum/10.0printf(平均值为:%f\n,ave);}6实现从键盘输入两个整数,按从小到大次序输出这两个数。#includestdio.hvoidmain(){inta,b,tem;printf(inputtwonumbers:\n);scanf(%d,%d,&a,&b);if(ab){tem=a;a=b;b=tem;}printf(%d,%d,a,b);7从键盘输入三个整数,输出最大的那个数。#includestdio.hvoidmain(){inta,b,c,max;printf(请输入三个整数:\n);scanf(%d%d%d,&a,&b,&c);max=ab?a:b;max=maxc?max:c;printf(“max=%d\n”,max);}}8输入任意一个三位数,将其各位数字反序输出(例如输入123,输出321)。#includestdio.hvoidmain(){intx,y,z,n;printf(pleaseinputanintegerlessthan1000\n);scanf(%d,&n);x=n/100;n=n-100*x;y=n/10;z=n-10*y;if(x0)printf(%d%d%d\n,z,y,x);elseif(y0)printf(%d%d\n,z,y);elseprintf(%d\n,z);}9输入三角形的边长,求三角形面积。(面积=sqrt(s(s-a)(s-b)(s-c)),s=(a+b+c)/2)。#includestdio.hvoidmain(){floata,b,c,s,area;intok=0;printf(inputa,b,c:);scanf(%f,%f,%f,&a,&b,&c);{a+bc?ok++:0;a+cb?ok++:0;b+ca?ok++:0;ok==3(s=1/2*(a+c+b);area=sqrt(s*(s-a)*(s-b)*(s-c));printf(s=%f\n,area=%f\n,s,area);}10输入2个数,比较其大小,将较大的数输出。#includestdio.hvoidmain(){inta,b,c,max,min;printf(“Inputa=”);scanf(“%d”,&a);printf(“Inputb=”);scanf(“%d”,&b);If(ab)printf(“较大的数是%d\n”,a);elseprintf(“较大的数是%d\n”,b);}11输入任意三个整数,求它们的和及平均值。#include“stdio.hvoidmain(){inta,b,c,sum;floatave;scanf(%d%d%d,&a,&b,&c);sum=a+b+c;ave=(a+b+c)/3.0;printf(三个数的和为:%d\n,sum);printf(三个数的平均值为:%f\n,ave);}12求解方程ax2+bx+c=0的实数根。a,b,c由键盘输入,a≠0且b2-4ac0。#includestdio.h#includemath.hvoidmain(){floata,b,c,x1,x2;printf(请输入a,b,c的值:);scanf(%f%f%f,&a,&b,&c);if(b*b-4*a*c=0)printf(输入的系数不对,b2-4ac不大于0!程序退出!\n);else{x1=(-b+sqrt(b*b-4*a*c))/2*a;x2=(-b-sqrt(b*b-4*a*c))/2*a;printf(x1=%.2f\n,x1);printf(x2=%.2f\n,x2);}}13写一个程序,输入一个班学生的成绩,求全班的平均成绩。#includeiostreamusingnamespacestd;voidfailnum(intscore[],intnum){intn=0;for(inti=0;inum;i++){if(score[i]60)n++;}cout不及格人数:nendl;}voidaverage_num(intscore[],intnum){intsum=0,number=0;floatn=0;for(inti=0;inum;i++)sum=sum+score[i];n=(sum*1.0)/num;for(i=0;inum;i++){if(score[i]=n)number++;}cout平均分为:nendl;cout成绩在全班平均分及平均分之上的学生人数:numberendl;}voidstastics(intscore[],intnum){intn1=0,n2=0,n3=0,n4=0,n5=0;for(inti=0;inum;i++){if(score[i]60)n1++;elseif(score[i]=69)n2++;elseif(score[i]=79)n3++;elseif(score[i]=89)n4++;if(score[i]=90)n5++;}cout分数在60以下占:((n1*1.0)/num)*100%endl;cout分数在60-69占:((n2*1.0)/num)*100%endl;cout分数在70-79占:((n3*1.0)/num)*100%endl;cout分数在80-89占:((n4*1.0)/num)*100%endl;cout分数在90以上占:((n5*1.0)/num)*100%endl;}intmain(){intscore[35],num;cout输入全班总人数:;cinnum;cout输入内容(每个数不超出0-100),用空格分开:endl;for(inti=0;inum;i++)cinscore[i];failnum(score,num);average_num(score,num);stastics(score,num);return0;}14编写程序输出如下图形:(没有的!!!!)**********从键盘输入一个整数判断是否为素数。#includestdio.hvoidmain()inti,n;printf(pleaseinputapositivenumber:);scanf(%d,&n);for(i=2;i=n-1;i++){if(n%i==0)break;}if(i=n-1)printf(%d不是素数\n,n);elseprintf(%d不是素数\n,n);}15输入一个三位数,判断是否是一个“水仙花数”,水仙花数是指3位数的各位数字的立方和等于这个三位数本身。例如153=1*1*1+5*5*5+3*3*3。#includestdio.hvoidmain(){intx,a,b,c;scanf(%d,x);a=x/100;b=(x/10)%10;c=x%10;if(x==a*a*a+b*b*b+c*c*c)printf(YES);elseprintf(NOT);}16输入年号,判断它是否为闰年。#includestdio.hvoidmain(){inty;scanf(%d,&y);if(y%400==0||y%4==0&&y%100!=0)printf(Yes.\n);elseprintf(No.\n);}17用for循环语句求n!用递归法求n!#includestido.hintfac(intn);voidmain(){intn;printf(pleaseinputn(n=0):);scanf(%d,&n);printf(%d!=%d\n,n,fac(n));}intfac(intn){intt;if(n==0||n==1)t=1;elset=n*fac(n-1);elsereturn(t);}18用函数实现求最大公约数的程序。#includestdio.hintgcd(inta,intb);voidmain(){inta,b,m;printf(pleaseinputa,b:);scanf(%d,%d,&a,&b);m=gcd(a,b);printf(gcd(%d,%d)=%d\n,a,b,m);}intgcd(inta,intb){inttdo{t=a%d;a=b;b=t;}while(b!=0);return(a);}19用函数实现求最小公倍数的程序。#includestdio.hintgcd(inta,intb);intlcm(inta,intb);voidmain(){inta,b,m,n;printf(pleaseinputa,b:);scanf(%d,%d,&a,&b);m=gcd(a,b);n
本文标题:C语言上机练习题及答案
链接地址:https://www.777doc.com/doc-4637370 .html