计算机程序设计基础(C 语言)编程练习题及参考答案1
输入 2 个整数,求两数的平方和并输出
#include 〈stdio
h> 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 %d\n",s); } 2
输入一个圆半径(r)当 r〉=0 时,计算并输出圆的面积和周长,否则,输出提示信息
#include #define PI 3
14 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 %f\n",s);printf("the circumference is %f\n”,l);}else printf(”input error
\n"); } 3、函数 y=f(x)可表示为: 2x+1 (x〈0) y= 0 (x=0) 2x-1 (x>0) 编程实现输入一个 x 值,输出 y 值
main(){int x,y;scanf(“%d”,&x);If(x〈0)y=2*x+1;If(x>0)y=2*x-1;If(x==0) y=0;printf(“%d",y);}4、编写一个程序,从 4 个整数中找出最小的数,并显示此数
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