资源描述:
《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;pri
2、ntf("pleaseinputr:");scanf("%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)编程实现输入一个x值,输出y值。#includevoidmain(){intx,y;第17页
3、共17页scanf(“%d”,&x);if(x<0)y=2*x+1;elseif(x>0)y=2*x-1;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(“m
4、in=%d”,a);}5.有一函数当x<0时y=1,当x>0时,y=3,当x=0时y=5,编程,从键盘输入一个x值,输出y值。#includevoidmain(){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.从键盘输入两个数,求出其最大值(要求使用函数完成求最大值,并在主函数中调用该函数)#includefloatmax(float
5、x,floaty);voidmain(){floata,b,m;scanf("%f,%f",&a,&b);第17页共17页m=max(a,b);printf("Maxis%f",m);}floatmax(floatx,floaty){floattemp;if(xvoidmain(){intyourAge,hisAge;p
6、rintf("Pleaseenteryourage:");scanf("%d",&yourAge);/*输入你的年龄yourAge*/printf("Pleaseenteryourfriend'sage:");scanf("%d",&hisAge);/*输入你朋友的年龄hisAge*/if(yourAge>=hisAge) {printf("Youareolder!Yourageis=%d",yourAge); }if(hisAge>yourAge) {printf("Yourfriendis
7、older!HisAgeageis=%d",hisAge); }}8、键盘输入2个加数,再输入答案,如果正确,显示“right”,否则显示“error”#include“stdio.h”voidmain(){inta,b,c;printf(“pleaseinputaandb”);scanf(%d,%d”,&a,&b);printf(“pleaseinputtheanswerfora+b”);scanf(%d”,&c);第17页共17页if(c==a+b)printf(“right”
8、);elseprintf(“error”);}9.编一程序每个月根据每个月上网时间计算上网费用,计算方法如下:要求当输入每月上网小时数,显示该月总的上网费用(6分)#includevoidmain(){inthour;floatfee;printf(“pleaseinputhour:”);scanf(“%d”,&hour);if(hour<=10)fee=30;elseif(hour>=10&&hour<=50)fee=3*hour;elsefee=hou