C语言程序设计教程习题答案(1~5)主审 郭浩志 北京邮电大学出版社 下载本文

习题4

1.0; 2.9;

3.20

#include main()

{ int a,b,x,y; scanf(\ if(a<=b) { x=1; printf(\ else {y=2; printf(\ } } 7.

#include main()

{ int ch1,ch2; ch1=getchar(); ch2=getchar(); if((ch1-ch2)/2==0) { putchar(ch1+1); putchar('\\n'); putchar(ch2+2); putchar('\\n'); } else { putchar(ch1-1); putchar('\\n'); putchar(ch2-1); putchar('\\n'); } } 8.

#include main()

{ int a,b; scanf(\

if(a%b==0) printf(\ else printf(\??%d\\n\} 9.

#include #include main()

{ float x,y,l; scanf(\ l=sqrt((x-2)*(x-2)+(y-2)*(y-2)); if(l>1) printf(\点A在圆外\\n\ else if(l=1) printf(\点A在圆上\\n\ else printf(\点A在圆内\\n\}

12.不嵌套的if语句

#include main()

{ int x,y; scanf(\ if(x<8) if(x>0) y=x+1; else if(x==0) y=0; else if(x>-5) y=x-1; printf(\}

嵌套的if语句: #include main()

{ int x,y; scanf(\ if(x<=0) if(x==0) y=0;

}

else if(x>-5) y=x-1; else if(x<8) y=x+1; printf(\

习题5

1.

#include void main()

{ int n,x=0,sum=0,i=-1; float average; scanf(\ while(x<=n) { sum+=x; x+=2; i++; } average=sum/i; printf(\} 2.

#include int main()

{ char ch; int let = 0; int num = 0; while ((ch = getchar()) != '*') //输入*结束 { if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) //如果是字母,let++ { let++; } else if (ch >= '0' && ch <= '9') //如果是数字,num++ { num++; } } printf(\字母:%d\\n\ //打印字母数 printf(\数字:%d\\n\ //打印数字数 return 0;} }(来源:百度账号terranlong) 3.

#include void main()

{ int n,i,a,sum=0,m; scanf(\ m=n; for(i=1;(m/i)!=0;) { a=n; sum+=a; n=(n-a)/10; i*=10;

} printf(\} 4.

#include int main() { char ch; do {ch= getchar(); if(ch<='z'&&ch>='a') printf(\ if(ch<='Z'&&ch>='A') printf(\ } while(ch<='z'&&ch>='A'); putchar('\\n'); } 5.

#include main()

{ float sum=200; int n=0; while(sum<500) { sum*=1.045; n++; } printf(\} 6.

#include int n;

void main() { int x=0,y; int shuzi(int n); for(n=1;n<=999;n++) { while(n%3==0) { y=shuzi(n); if(y!=0) { x++; printf(\ break; } break; }