第四章自测练习答案 清华大学出版社 C语言习题、实验指导及课程设计

更新时间:2023-09-04 07:35:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第四章 循环习题答案

一 填空题

1, 输出结果:0,10<回车>1,9<回车>2,8<回车>3,7<回车>4,6<回车>5,5<回车> 2, 输出结果: 0 2 4 6 8

3, 输入5则输出结果:5<回车>25<回车>125<回车>625<回车>

输入6则输出结果:6<回车>36<回车>216<回车>1296<回车>

输入7则输出结果:7<回车>49<回车>343<回车>2401<回车>

4, 输入5则输出结果: 25<回车>125<回车>625<回车>3125<回车>

输入6则输出结果: 36<回车>216<回车>1296<回车>7776<回车>

输入7则输出结果: 49<回车>343<回车>2401<回车>16807<回车>

5, 修改如下:

count = 1; sum=0;

while (count <= 5)

{ count += 1;

printf("Next number>");

scanf(“%d”,&next_num);

sum += next_num;

printf("%d number were added; \n",count);

}

printf("their sum is %d. \n",sum);

6, 值如下: sum=1,odd=1; sum=4,odd=3; sum=9,odd=5; sum=16,odd=7;

7, 题目少图,无法做;

8, while最少0次;for最少0次;

9, n=32,m=12,p=11;

10, i=i+1;j=j-1;n=i+j,m=i+j;j=j-1;p=i+j;

11, 修改:for( mult5 = 0; mult5 < 100; mult5 += 5)printf (" %d \n" , mult5); 12, 输出结果:1 10<回车>2 8<回车>3 6<回车>4 4<回车>5 2<回车>

二 选择题

1-6CABADCD

8-10B,BB,BC

三 填空题

1, 1 1

2, a!=0

3, n=n/10

4, 9

5, i<10 j%3!=0

6, $ &&&*

7, *#*#*#$

8, 669877

9, ch=ch+1 printf(“\n”);

四 编程题

1,

#include "stdio.h"

void main()

{ int i,result=1;

for(i=0;i<7;i++)

{ printf("%3d%3d\n",i,result);

result=result*2;

}

}

2,

#include "stdio.h"

void main()

{ int i,sum=0;

for(i=1;i<100;i=i+2)

sum=sum+i;

printf("%3d\n",sum);

}

3,

#include "stdio.h"

#define RATE 2.54

void main()

{ int mininch,maxinch,showinch,i;

printf("please input the min and max inch:\n");

scanf("%d%d",&mininch,&maxinch);

printf("print inch and centimetre every 6 inchs:\n");

showinch=mininch;

while(showinch<=maxinch)

{ printf("%3d inch= %3d centimetre\n",showinch,showinch*RATE); showinch+=6;

}

}

4,

#include "stdio.h"

#define RATE 1.1

void main()

{ int person=9870,count_years=1,i=1,futureperson;

futureperson=person;

while(futureperson<=30000)

{ printf("in %3d th year,%3d person\n",count_year,futureperson); futureperson*=RATE;

count_year++;

}

printf("after %3d years,person number will passed 3000\n",count_year);

}

5,

#include "stdio.h"

#define RATE 2

#define PRICE 0.8

void main()

{ int day=1,applenumber=1;

float money=PRICE,datecost;

while(applenumber*RATE<=100)

{ day++;

applenumber*=RATE;

money=applenumber*PRICE; }

datecost=money/day;

printf("cost %.2f money every day\n",datecost); }

6,

#include "stdio.h"

void main()

{ int read;

scanf("%d",&read);

if((read%5==0) &&(read%7==0))

printf("yes\n");

else printf("no\n");

}

7,

#include "stdio.h"

void main()

{ double monthfall, sumfall=0,avefall; int month=1;

while(month<13)

{ scanf("%f",&monthfall);

sumfall+=monthfall;

month++;

}

avefall=sumfall/month;

printf("%f,%f\n",sumfall,avefall);

}

8,

#include "stdio.h"

void main()

{ int time,ascnumber;

char ch;

for(time=1;time<11;time++) {getchar(ch);

pritf("%3d\n",ch);

}

}

9,

#include "stdio.h"

void main()

{ long term=1,sum=0,i;

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

{ sum+=term;

term*=i;

}

printf("result is %ld\n", sum); }

10,参照课本例题;

11,参照课本例题;

12,

#include "stdio.h"

void main()

{ int demo=1,i=1;

float add=1,sum=1;

while(fabs(add)<=1e-6)

{ sum+=add;

demo=i*(i+1);

add=1.0/demo;

i++;

}

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

13,

#include "stdio.h"

void main()

{int i,j;

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

{for(j=1;j<=10-i;j++)

printf("%3d*%3d=%3d",i,j,i*j);

printf("\n");

}

}

14,

#include "stdio.h"

void main()

{ int n=1;

float p,c,d,k;

scanf("%f%f%f",&c,&d,&p); k=c;

{k=k*(1-d);

n++;}

while(k<=p)

printf("it can be used %d years\n",n-1); }

本文来源:https://www.bwwdw.com/article/mwgi.html

Top