《C语言上机练习题及答案.doc》由会员分享,可在线阅读,更多相关《C语言上机练习题及答案.doc(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流C语言上机练习题及答案【精品文档】第 9 页1输入三个整数x,y,z,把这三个数由小到大输出。# include void main() int x,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,%dn,x,y,z); 2输入圆的半径,输出圆的周长和面积。# include # define PI 3.1415926# define S =PI*r*r# define V (4.0/
2、3)*PI*r*r*r void main() double r; printf(please input r:); scanf(%lf,&r); printf(area is %.4lfn,S); printf(volume is %.4lfn,V);输入正方形的边长,输出正方形的周长和面积。# includevoid main()float c,zc,mj;printf(输入你的正方形);scanf(%fn,&c); zc=4*c;mj=c*c;printf(周长%f,面积%f,边长%f,zc,mj,c);3用格式输入函数输入3个字符,并用输出函数反向输出3个字符和他们的ASCII码。#
3、includeint main()char-ch1,ch2,ch3;printf(please input three characters:n);scanf(%C%C%c,&ch1,&ch2,&ch3);printf(%cn%dn,ch3,ch3);printf(%cn%dn,ch2,ch2);printf(%cn%dn,ch1,ch1);输入一个摄氏温度,要求输出华氏温度。公式为f=9/5*c+32。# include void main() float C,F; scanf(%f,&F); C=5.0/9*(F-32); printf(%8.2f,C);4。比较两个数大小的题#inclu
4、de void main() int a,b; scanf(%d,%d,&a,&b); if(ab) printf(%dn,a); else printf(%dn,b); 第二题: 此题作废(千万不要用)#includevoidmain() char a,b,c; scanf(%c,%c,%c,&a,&b,&c); while(a122)|(b122)|(c122);printf(您输入非三个小写字母,请重新输入:n); scanf(%c,%c,%c,&a,&b,&c); printf(%c,%c,%cn,a-32,b-32,c-32);5输入任意10个数,求他们的平均值。# include
5、void main() Printf( 请输入10个整数: );int a10, k = 0, sum = 0; for (int i = 0; i ai;if 10=ai 0 +k; sum = sum+ai; float ave=sum / 10 .0printf( 平均值为:%fn,ave);6实现从键盘输入两个整数,按从小到大次序输出这两个数。# include void main()int a, b, tem; printf(input two numbers:n); scanf(%d,%d,&a,&b ); if(ab) tem=a;a=b;b=tem;printf (%d,%d,
6、a,b);7从键盘输入三个整数,输出最大的那个数。#include void main() int a,b,c,max; printf(请输入三个整数:n); scanf(%d%d%d,&a,&b,&c);max=ab?a:b;max=maxc?max:c;printf(“max=%dn”,max);8输入任意一个三位数,将其各位数字反序输出(例如输入123,输出321)。# include void main() int x,y,z,n; printf(please input an integer less than 1000n); scanf(%d,&n); x=n/100; n=n-1
7、00*x; y=n/10;z=n-10*y; if (x0)printf(%d%d%dn,z,y,x); else if(y0) printf(%d%dn,z,y); else printf(%dn,z);9输入三角形的边长,求三角形面积。(面积=sqrt( s ( s-a ) (s-b) (s-c) ),s=(a+b+c)/2 )。# include void main() float a,b,c,s,area; int ok=0; printf(input a,b,c:); scanf (%f,%f,%f,&a,&b,&c); a+bc? ok+:0; a+cb? ok+:0; b+ca?
8、 ok+:0; ok=3 (s=1/2*(a+c+b); area=sqrt (s*(s-a)*(s-b)*(s-c) );printf (s=%fn,area=%fn,s,area);10输入2个数,比较其大小,将较大的数输出。#includevoid main()int a,b,c,max,min;printf(“Input a=”);scanf(“%d”,&a);printf(“Input b=”);scanf(“%d”,&b);If(ab)printf(“较大的数是%dn”,a);elseprintf(“较大的数是%dn”,b);11输入任意三个整数,求它们的和及平均值。#includ
9、e“stdio.h void main() int a,b,c,sum; float ave; scanf(%d %d %d,&a,&b,&c); sum=a+b+c; ave=(a+b+c)/3.0; printf(三个数的和为: %dn,sum); printf(三个数的平均值为: %fn,ave);12求解方程ax2+bx+c=0的实数根。a,b,c由键盘输入,a0且b2-4ac0。#include #include void main() float a, b, c, x1, x2; printf(请输入a, b, c的值:); scanf(%f%f%f, &a, &b, &c); i
10、f (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 = %.2fn, x1); printf(x2 = %.2fn, x2); 13写一个程序,输入一个班学生的成绩,求全班的平均成绩。#include using namespace std; void failnum(int score, int num) int n = 0; for(int i = 0; i num; i
11、+) if(scorei 60) n+; cout不及格人数:nendl; void average_num(int score, int num) int sum = 0, number = 0; float n = 0; for(int i = 0; i num; i+) sum = sum + scorei; n = (sum * 1.0) / num; for(i = 0; i = n) number+; cout平均分为:nendl; cout成绩在全班平均分及平均分之上的学生人数:numberendl; void stastics(int score, int num) int n
12、1 = 0, n2 = 0, n3 = 0, n4 = 0, n5 = 0; for(int i = 0; i num; i+) if(scorei 60) n1+; else if(scorei = 69) n2+; else if(scorei = 79) n3+; else if(scorei = 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分数在8
13、0-89占 :( (n4* 1.0) / num)*100%endl; cout分数在90以上占:( (n5* 1.0) / num)*100%endl; int main() int score35, num; coutnum; cout输入内容(每个数不超出 0-100),用空格分开:endl; for(int i = 0; i scorei; failnum(score, num); average_num(score, num); stastics(score, num); return 0; 14编写程序输出如下图形:(没有的!)从键盘输入一个整数判断是否为素数。# includes
14、tdio.h void main() int i,n; printf(please input a positive number:); scanf(%d,&n);for(i=2;i=n-1;i+) if(n%i=0)break; if(i=n-1) printf(%d不是素数n,n); else printf(%d不是素数n,n);15输入一个三位数,判断是否是一个“水仙花数”,水仙花数是指3位数的各位数字的立方和等于这个三位数本身。例如153=1*1*1+5*5*5+3*3*3。#include void main() int x,a,b,c; scanf(%d,x); a=x/100;
15、b=(x/10)%10; c=x%10; if(x=a*a*a+b*b*b+c*c*c) printf(YES); else printf(NOT); 16输入年号,判断它是否为闰年。#include void main() int y; scanf(%d,&y); if(y%400=0|y%4=0&y%100!=0) printf(Yes.n); else printf(No.n);17用for循环语句求n! 用递归法求n! # include int fac(int n); void main()int n; printf(please input n(n=0):); scanf(%d,&
16、n); printf(%d!=%dn,n,fac(n); int fac(int n) int t; if (n=0|n=1) t=1; else t=n*fac(n-1); else return (t);18用函数实现求最大公约数的程序。# include int gcd(int a,int b);void main()int a,b,m;printf(please input a,b:);scanf(%d,%d,&a,&b);m=gcd (a,b);printf(gcd(%d,%d)=%dn,a,b,m);int gcd(int a,int b)int tdot=a%d;a=b;b=t;
17、while(b!=0);return(a);19用函数实现求最小公倍数的程序。# include int gcd(int a,int b);int lcm(int a,int b);void main()int a,b,m,n;printf(please input a,b:);scanf(%d,%d,&a,&b);m=gcd (a,b);n=lcm(a,b);printf(gcd(%d,%d)=%dn,a,b,m);printf(lcm(%d,%d)=%dn,a,b,n);int gcd(int a,int b)int tdot=a%d;a=b;b=t;while(b!=0);return(
18、a);int lcm(int a,int b)int t;t=a*b/gcd(a,b);return (t);20编写判断水仙花的函数,从主函数输入一个三位正整数N,在主函数中调用判断水仙花数的函数,求出三位数中所有的水仙花数。水仙花数是指3位数的各位数字的立方和等于这个三位数本身。例如153=1*1*1+5*5*5+3*3*3。#include void f(m) int x,y,z;x=m/100;y=m/10%10;z=m%10;if(m=x*x*x+y*y*y+z*z*z)printf(%d是水仙花数n ,m); else printf(%d不是水仙花数n ,m);void main
19、() int n;scanf(%d, &n); f(n);21将一个正整数逆续输出。# include void main()int n,digit;scanf(%d,&n);while(n!=0)digit=n%10;printf(%dn:,digit);n=n/10; 22 从键盘输入一个正整数n,计算该数的各位数字之和并输出。例如,输入数是5246,则计算5+2+4+6=17并输出。 # include void main() int x,s=0,digit; scanf(%d,&x); while(x!=0) digit=x%10; 循环x=x/10;s=s+digit;printf(
20、%dn,s); 23输出九九乘法表。#includevoid main() int a,b; for (a=1;a=9;a+) ;for(b=1;b=a;b+) ;printf(%d*%d=%-2d ,a,b,a*b); printf(n); 23用数组实现从键盘输入10个数,并且输出这10个数。 #include void main () int a10,*p=a,i; printf(Iput 10 numbers:n);for (i=0;i10; i+)scanf(%d,p+i);printf(a10:);for (i=0;i10; i+)printf(%3d,*(p+i);printf(n);