C语言练习题带答案 下载本文

内容发布更新时间 : 2024/12/23 20:27:25星期一 下面是文章的全部内容请认真阅读。

4.

#include main()

{ float a,b,c,t; a=3; b=7; c=1; if(a>b)

{t=a;a=b;b=t;} if(a>c)

{t=a;a=c;c=t;} if(b>c)

{t=b;b=c;c=t;}

printf(\}

运行结果为:1.00, 2.00, 7.00 5.

#include < stdio .h > main ( )

{ float c=3.0 , d=4.0; if ( c>d ) c=5.0; else

if ( c==d ) c=6.0; else c=7.0;

printf ( “%.1f\\n”,c ) ; }

运行结果为:7.0 6.

#include main() { int m; scanf(\ if (m >= 0) { if (m%2 == 0) printf(\ else printf(\} else

{ if (m % 2 == 0) printf(\

else printf(\}

若键入-9,则运行结果为: -9 is a negative odd

7.

#include main()

{ int num=0;

while(num<=2){ num++;printf(\;} }

运行结果为: 1 2 3 8.

#include main( )

{ int sum=10,n=1;

while(n<3) {sum=sum-n; n++; } printf(“%d,%d”,n,sum); }

运行结果为:3,7 9.

#include main()

{ int num,c;

scanf(\

do {c=num; printf(\ }while((num/=10)>0); printf(\ }

从键盘输入23,则运行结果为:32 10

#include main()

{ int s=0,a=5,n; scanf(\

do { s+=1; a=a-2; }while(a!=n); printf(\,%d\\n\ }

若输入的值1,运行结果为: 2,1

11.

#include \ main() {char c; c=getchar();

while(c!='?') {putchar(c); c=getchar(); } }

如果从键盘输入abcde?fgh(回车) 运行结果为:abcde

12.

#include main() { char c;

while((c=getchar())!=’$’)

{ if(‘A’<=c&&c<=‘Z’) putchar(c);

else if(‘a’<=c&&c<=‘z’) putchar(c-32); }

当输入为ab*ABí#CD$时,运行结果为:ABABCDCD

13.

#include main()

{ int x, y =0;

for(x=1;x<=10;x++)

{ if(y>=10)

break; y=y+x; }

printf(“%d %d”,y,x); }

运行结果为:10 5 14.

#include main( ) { char ch;

ch=getchar( ); switch(ch)

{ case ‘A’ : printf(“%c”,’A’);

case ‘B’ : printf(“%c”,’B’); break; default: printf(“%s\\n”,”other”); } }

当从键盘输入字母A时,运行结果为:AB

} 15.

#include main( )

{ int a=1,b=0; scanf(“%d”,&a);

switch(a)

{ case 1: b=1;break; case 2: b=2;break; default : b=10;} printf(\; }

若键盘输入5,运行结果为:10 16.

#include main()_

{ char grade=’C’; switch(grade)

{ case ‘A’: printf(“90-100\\n”); case ‘B’: printf(“80-90\\n”);

case ‘C’: printf(“70-80\\n”);

case ‘D’: printf(“60-70\\n”); break; case ‘E’: printf(“<60\\n”); default : printf(“error!\\n”);

} }

运行结果为: 70-80 60-70 17.

#include main() { int y=9;

for(;y>0;y- -)

if(y%3==0)

{ printf(%d”,- -y); continue; }

}

运行结果为: 852

18.

#include main()

{ int i,sum=0; i=1;

do{ sum=sum+i; i++; }while(i<=10); printf(“%d”,sum); }

运行结果为: 55 19.

#include #define N 4 main() { int i;

int x1=1,x2=2; printf(\

for(i=1;i<=N;i++)

{ printf(\if(i%2==0)

printf(\x1=x1+x2; x2=x2+x1; } }

运行结果为: 1 2 3 5 8 13 21 34 20

#include main( )

{ int x, y;

for(x=30, y=0; x>=10, y<10; x--, y++) x/=2, y+=2;

printf(“x=%d,y=%d\\n”,x,y); }

运行结果为: x=0,y=12