资源描述:
《C语言经典例题及答案.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C语言经典例题及答案作业一一、从键盘上输入三个数,求出其中的最大值,并输出。#includevoidmain(){inta,b,c,max;scanf("%d%d%d",&a,&b,&c);max=a;if(max#definePI3.14159#includevoid
2、main(){floata,b,sum;a=30*PI/180;b=60*PI/180;sum=sin(a)+sin(b)+cos(a)+cos(60);printf("total=%f",sum);}三、比较两个数的大小。如果x大于y,则输出:x>y,否则输出:xvoidmain(){intx,y;scanf("%d,%d",&x,&y);if(x>y)printf("x>y");elseprintf("x3、evoidmain(){inta,b,c,t;scanf("%d%d%d",&a,&b,&c);if(a>b){t=b;b=a;a=t;}if(a>c){t=a;a=c;c=t;}if(b>c){t=b;b=c;c=t;}printf("%3d%3d%3d",a,b,c);}.一、求自然数1~10之和。。#includevoidmain(){inti,sum;i=1;sum=0;while(i<=10){sum=sum+i;i++;}printf("sum=%d",sum);}二、输入10个整
4、数,统计出正数之和以及负数之和。(求奇数之和以及偶数之和)#includevoidmain(){intsum1=0,sum2=0,i,x;i=1;while(i<=10){scanf("%d",&x);if(x%2==0)sum1=sum1+x;elsesum2=sum2+x;i++;}printf("sum1=%dsum2=%d",sum1,sum2);}一、求一个任意边长的矩形面积。#includevoidmain(){intw,h,sum;scanf("%d%d",&w,&h);sum
5、=w*h;printf("area=%d",sum);}二、求一个任意半径的圆的面积及周长。#definePI3.14159#includevoidmain(){floatr,area,c;scanf("%f",&r);area=PI*r*r;c=2*PI*r;printf("area=%fcircle=%f",area,c);}三、已知:w=5,y=4,z=2,求表达式:w*y/z的值,并输出。##includevoidmain(){intw,y,z,r;w=5;y=4;z=2;r=w
6、*y/z;printf("%5d",r);}一、求一个任意边长的矩形面积。#includevoidmain(){intw,h,sum;scanf("%d%d",&w,&h);sum=w*h;printf("area=%d",sum);}二、求一个任意半径的圆的面积及周长。#definePI3.14159#includevoidmain(){floatr,area,c;scanf("%f",&r);area=PI*r*r;c=2*PI*r;printf("area=%fcircle=%f",
7、area,c);}一、已知:w=5,y=4,z=2,求表达式:w*y/z的值,并输出。##includevoidmain(){intw,y,z,r;w=5;y=4;z=2;r=w*y/z;printf("%5d",r);}作业四一、输入一个0~6的整数,转换成星期输出。#includemain(){inta;scanf("%d",&a);switch(a){case0:printf("Sunday");break;case1:printf("Monday");break;case2:printf("
8、Tuesday");break;case3:printf("Wednesday");break;case4:printf("Thursday");break;case5:printf("Friday");b