2024年3月12日发(作者:)
复习题
C程序设计编程题
1。输入2个整数,求两数的平方和并输出。
#include void main() { intt a ,b,s; printf(”please input a,b:n”); scanf(”%d%d”,&a,&b); s=a*a+b*b; printf("the result is %dn",s); } 2. 输入一个圆半径(r),当r>=0时,计算并输出圆的面积和周长,否则,输出 提示信息。 #include 〈stdio.h> #define PI 3。14 void main() { float r ,s , l; printf(”please input r:n”); scanf("%f",&r); if (r〉=0) {s=pi*r*r; l=2*i*r ; printf(”the area is %fn”,s); printf(”the circumference is %fn”,l);} else printf("input error!n”); } 3、函数y=f(x)可表示为: 2x+1 (x<0) y= 0 (x=0) 2x-1 (x>0) 第 1 页 共 17 页 编程实现输入一个x值,输出y值。 #include 〈stdio。h> void main() { int x,y; scanf(“%d",&x); if(x<0) y=2*x+1; else if(x〉0) y=2*x—1; else y=0; printf(“%d”,y);} 4、编写一个程序,从4个整数中找出最小的数,并显示此数. #include void main( ) {int a,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 n”,a); } 5.有一函数当x〈0时y=1,当x>0时,y=3,当x=0时y=5,编程,从键盘输入一个x 值,输出y值。 #include void main() {int x,y; scanf("%d",&x); if (x〈0) y=1; else if(x==0) y=5; else y=3; printf("x=%d,y=%dn”,x,y);} 6.从键盘输入两个数,求出其最大值(要求使用函数完成求最大值,并在主函数中 调用该函数) 第 2 页 共 17 页


发布评论