资源描述:
《C语言基本编程题》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、复习题C程序设计编程题1.输入2个整数,求两数的平方和并输出。#includevoidmain(){intta,b,s;printf("pleaseinputa,b:");scanf("%d%d”,&a,&b);s=a*a+b*b;printf("theresultis%d",s);}2.输入一个圆半径(r),当r>=0时,计算并输出圆的面积和周长,否则,输出提示信息。#include#definePI3.14voidmain(){floatr,s,l;printf("pleaseinputr:");sc
2、anf("%f”,&r);if(r>=0){s=pi*r*r;l=2*i*r;printf("theareais%f",s);printf("thecircumferenceis%f",l);}elseprintf("inputerror!");}3、函数y=f(x)可表示为:2x+1(x<0)y=0(x=0)2x-1(x>0)第17页共17页编程实现输入一个x值,输出y值。#includevoidmain(){intx,y;scanf(“%d”,&x);if(x<0)y=2*x+1;elseif(x>0)y=2*x-1;
3、elsey=0;printf(“%d”,y);}4、编写一个程序,从4个整数中找出最小的数,并显示此数。#includevoidmain(){inta,b,c,d,t;scanf(“%d,%d,%d,%d”,&a,&b,&c,&d);if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(a>d){t=a;a=d;d=t;}printf(“min=%d”,a);}5.有一函数当x<0时y=1,当x>0时,y=3,当x=0时y=5,编程,从键盘输入一个x值,输出y值。#include4、>voidmain(){intx,y;scanf("%d",&x);if(x<0)y=1;elseif(x==0)y=5;elsey=3;printf("x=%d,y=%d",x,y);}6.从键盘输入两个数,求出其最大值(要求使用函数完成求最大值,并在主函数中调用该函数)第17页共17页#includefloatmax(floatx,floaty);voidmain(){floata,b,m;scanf("%f,%f",&a,&b);m=max(a,b);printf("Maxis%f",m);}floatmax(float
5、x,floaty){floattemp;if(xvoidmain(){intyourAge,hisAge;printf("Pleaseenteryourage:");scanf("%d",&yourAge);/*输入你的年龄yourAge*/printf("Pleaseenteryourfriend'sage:");scanf("%d",&hisAge);/*输入你朋友的年龄hi
6、sAge*/if(yourAge>=hisAge) {printf("Youareolder!Yourageis=%d",yourAge); }if(hisAge>yourAge) {printf("Yourfriendisolder!HisAgeageis=%d",hisAge); }}8、键盘输入2个加数,再输入答案,如果正确,显示“right”,否则显示“error”#include“stdio.h”voidmain()第17页共17页{inta,b,c;printf(“pleaseinputaandb”);scanf(%d,%d”,
7、&a,&b);printf(“pleaseinputtheanswerfora+b”);scanf(%d”,&c);if(c==a+b)printf(“right”);elseprintf(“error”);}9.编一程序每个月根据每个月上网时间计算上网费用,计算方法如下:要求当输入每月上网小时数,显示该月总的上网费用(6分)#includevoidmain(){inthour;floatfee;printf(“pleaseinputhour:”);scanf(“%d”,&hour);if(hour<=10)fee=3
8、0;elseif(hour>=10&&hour<=50)fee=3*hour;elsefee=