资源描述:
《C语言经典例题与答案解析》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、作业一一、求一个任意边长的矩形面积。#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",area,c);}三、已知:w=5
2、,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);}作业二一、从键盘上输入三个数,求出其中的最大值,并输出。#includevoidmain(){inta,b,c,max;scanf("%d%d%d",&a,&b,&c);max=a;if(max3、0+sin60+cos30+cos60之和。(注意:30*3.14159/180)#include#definePI3.14159#includevoidmain(){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;sca
4、nf("%d,%d",&x,&y);if(x>y)printf("x>y");elseprintf("xvoidmain(){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之和。。#include5、dio.h>voidmain(){inti,sum;i=1;sum=0;while(i<=10){sum=sum+i;i++;}printf("sum=%d",sum);}三、输入10个整数,统计出正数之和以及负数之和。(求奇数之和以及偶数之和)#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
6、=%d",sum1,sum2);}作业四一、输入一个0~6的整数,转换成星期输出。#includemain(){inta;scanf("%d",&a);switch(a){case0:printf("Sunday");break;case1:printf("Monday");break;case2:printf("Tuesday");break;case3:printf("Wednesday");break;case4:printf("Thursday");break;case5:printf("Friday"
7、);break;case6:printf("saturday");break;default:printf("Iamsorry.Youarewrong.");}}.二、输入一个整数,将各位数字反转后输出。如:输入365,则输出显示为563。#includevoidmain(){intx,r;scanf("%d",&x);printf("converis:");while(x!=0){r=x%10;printf("%d",r);x=x/10;}printf("");}三、输入一个整数,求出它的所有因子。#in
8、cludevoidmain(){intx,i;scanf("%d",&x);for(i=1;i<=x;i++)if(x%i==0)printf("%3d",i);}作业五一、求100~200之间的素数。