C语言习题及解答

C语言习题及解答
C语言习题及解答

C 语言习题及解答

1、输入一个华氏温度,要求输出摄氏温度。公式为#include void main( ) {float C,F;

printf("Input F:"); scanf("%f",&F); C=5.0/9*(F-32); printf("C=%.2f\n",C);}

2、编写程序,从键盘输入一个大写字母,将它转

换为对应的小写字母后输出。(提示:同一个字母的大写比小写小32)

#include void main( ) { char ch;

printf("Input ch:"); scanf("%c",&ch); ch=ch+32;

printf(“ch=%c \n",ch);}

3、编写程序,输入梯形的上底、下底和高,计算

并输出梯形的面积。

#include void main( ) { float a,b,h,area; printf("Input a,b,h: "); scanf("%f%f%f", &a,&b,&h); area=(a+b)*h/2;

printf("area=%.2f\n", area); }

4、编写程序,输入圆半径r ,求圆周长、圆面积、

圆球表面积、圆球体积。

#include #define PI 3.1415926 void main( ) {float r,L,s1,s2,V; printf("Input r:"); scanf("%f", &r); L=2*PI*r; s1=PI*r*r; s2=4*PI*r*r; V=4.0/3*PI*r*r*r;

printf("L=%.2f, s1=%.2f, s2=%.2f, V=%.2f\n", L,s1,s2,V);

}

5、有三个电阻r1、r2、r3并联,编写程序计算并

输出并联后的电阻r 。已知电阻并联公式为:

#include void main( ) { float r,r1,r2,r3;

printf("Input r1,r2,r3: "); scanf("%f%f%f", &r1,&r2,&r3); r=1/(1/r1+1/r2+1/r3); printf("r=%.2f\n", r); }

6、由键盘输入一个10-99之间的整数,将该数分

解,分别输出其个位数字和十位数字。例如,输入

85,输出:5,8。

提示:用算术运算中的整除和取余运算实现。

#include void main( )

{int x, a, b; scanf("%d", &x); a=x%10; b=x/10;

printf(“a=%d, b=%d \n", a, b);}

7、编写程序,输入三角形的三条边,计算并输出

三角形的面积。(注意输入的三条边必须要能构成一个三角形)求三角形的面积公式为 其中s=(a+b+c)/2。

#include #include void main( )

scanf("%f%f%f", &a,&b,&c); s= (a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("area=%.2f\n", area);

8、周期为T 秒的人造卫星离地面

的平均高度H 的计算公式为:

其中:M=6×1024kg 是地球质量,R=6.371×106m 是地球半径。

编写程序,输入人造卫星的周期T ,计算并输出人造卫星离地面的高度H 。

算法提示:求xy 结果的数学函数是pow(x, y)

#include #include #define PI 3.1415926 void main( )

{double M=6E24,R=6.371E6,T,H,x;

()329

5

-=

F C 3

121111r r r r ++=()()()

c s b s a s s area ---=R

MT H -?=-32

2

1141067.6π

printf("Input T: ");

scanf("%lf",&T);

x=6.67E-11*M*T*T/(4*PI*PI);

H=pow(x,1.0/3)-R;

printf("H=%.2E\n", H);

9、求任意三个整数的平均值。要求:输入数据与输出结果都应有相应的提示信息。且输出数据取小数点后两位数字显示。

#include

void main( )

{float a,b,c,ave;printf(“Input 3 numbers:”);

scanf("%f%f%f", &a,&b,&c);

ave= (a+b+c)/3;

printf(“average=%.2f\n", ave);

10、输入一个字符,并输出。其中有一个条件是如果该字符是小写的英文字母,则需把它转换成大写字母再输出。

#include

void main( )

{char ch;

printf("Input ch: ");

scanf("%c", &ch);

if (ch>='a'&&ch<='z')

ch=ch-32;

printf("%c\n", ch);

}

11、输入年号,判断并输出该年是否为闰年。所谓闰年,是指能被4整除,但不能被100整除;或能被400整除的年份。

#include

void main( )

{int year;

printf("Input year: ");

scanf("%d",&year); if (year%4==0&&year%100!=0||year%400==0)

printf("%d is a leap year.\n",year);

else

printf("%d is not a leap year.\n",year);

12、编写程序,输入一个字符存入变量ch中,根据该字符的ASCII码值判断并输出字符的类型,即字母(alpha)、数字(numeric)或其他字符(other)

#include

void main( )

{ char ch;

printf("Input ch: ");

scanf("%c", &ch);

if ((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))

printf("alpha\n");

else if (ch>='0' && ch<='9')

printf("numeric\n");

else

printf("other\n");

}

13、有一个函数,编写程序,输入x的值,计算并

输出y值。

#include

#include

void main( )

{double x,y; printf("Input x: "); scanf("%lf", &x); if

(x<-1)

y=x*x*x-1; else if (x<=1) y=-3*x+1;

else if (x<=10)

y=3*exp(2*x-1)+5;

else y=5*x+3*log10(2*x*x-1)-13;

printf("y=%.2f\n", y);

14、从键盘输入三个数,代表三条线段的长度。请

编写程序,判断这三条线段组成的三角形是什么类

型(等边、等腰、不等边或不能构成三角形)。

#include

void main( )

{float a,b,c; printf("Input a,b,c:");

scanf("%f%f%f",&a,&b,&c);

if (a+b<=c || b+c<=a || c+a<=b)

printf("It is not a triangle!\n");

else if (a==b&&b==c)

printf("It is a equilateral triangle!\n");

else if (a==b||b==c||c==a)

printf("It is a isosceles triangle!\n");

else

printf("It is a common triangle!\n");

15、简单选择界面的编程,要求用switch实现多分

支。从键盘输入整数,输出不同的字符串:

输入1,输出Good morning;

输入2,输出Good afternoon;

输入3,输出Good evening;

输入4,输出Good night;

输入其它数字,输出Bye-bye。

#include

?

?

?

?

?

?

?

-

-

+

+

+

-

-

=

-

13

)1

2

lg(

3

5

5

3

1

3

1

2

1

2

3

x

x

e

x

x

y

x

 ()

()

()

()

10

10

1

1

1

1

>

<

-

-

<

x

x

x

x

相关主题
相关文档
最新文档