c语言第五章习题答案

  • 格式:doc
  • 大小:41.50 KB
  • 文档页数:7

第一题:

1. 从键盘输入10个数,求和。

#include "stdio.h"

void main( )

{

int x,s=0;

int i;

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

{scanf("%d",&x);

s+=x;}

printf("s=%d\n",s );

}

2. 从键盘输入10个数,求平均值。

#include "stdio.h"

void main( )

{

int x,s=0,ave;

int i;

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

{scanf("%d",&x);

s+=x;} ave=s/10;

printf("s=%d,ave=%d\n",s ,ave );

}

3. 从键盘输入10个数,求偶数的和。

#include "stdio.h"

void main( )

{

int x,s=0;

int i;

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

{scanf("%d",&x);

if(x%2==0) s+=x;}

printf("s=%d\n",s );

}

4. 从键盘输入10个数,求偶数的平均值。

#include "stdio.h"

void main( )

{

int x,s=0,ave;

int i;

int k=0; for(i=0;i<10;i++)

{scanf("%d",&x);

if(x%2==0) {s+=x;k++;}

}

ave=s/k;

printf("s=%d,ave=%d\n",s,ave );

}

5. 从键盘输入n个数,求偶数的平均值。

#include "stdio.h"

void main( )

{

int x,s=0,ave;

int i;

int k=0;

int n;

scanf("%d",&n);

for(i=0;i

{scanf("%d",&x);

if(x%2==0) {s+=x;k++;}

}

ave=s/k;

printf("s=%d,ave=%d\n",s,ave ); }

第二题:

#include "stdio.h"

void main( )

{

char c;

int zm=0,sz=0;

do

{c=getchar();

if(c>='A'&&c<='Z' || c>='a'&&c<='z')zm++;

else if(c>='0'&&c<='9')sz++;}while(c!='*');

printf("zm=%d,sz=%d\n",zm,sz );

}

第三题:

原数n 个位d 求和

5246 6 d=n%10 S=s+d

524 n=n/10 4

52 2

5 5

0 n==0 #include "stdio.h"

void main( )

{

int n,s=0,d,x;

scanf("%d",&n);

x=n;

while(n!=0)

{d=n%10;

s+=d;

n=n/10;}

printf("n=%d,s=%d\n",x,s );

}

第四题:

#include "stdio.h"

void main( )

{char c;

do

{c=getchar();

if(c >= 'A' && c <= 'Z') putchar(c+32);

else if(c >= 'a' && c < 'z' )putchar(c-32);

}while(c!='\n');

} 第五题:

#include "stdio.h"

void main( )

{int k=0; float t=200;

while(t<500)

{ t=t*(1+0.045);

k++;

//if(t>=500)break; }

printf("after %d years: t=%f",k,t);

}

第六题:

#include "stdio.h"

void main( )

{int m,d,x;

for(m=1;m<=999;m++)

{ x=m;

if(x%3==0)

{

while(x!=0)

{ d=x%10;

if(d==5) {printf("%4d",m);break;}

x=x/10; }

}

}

}

第七题:

#include "stdio.h"

void main( )

{int m,k=0;

for(m=7; 1 ;m++)

{

if(m%2==1&&m%3==2&&m%5==4&&m%6==5&&m%7==0)

{printf("m=%d\n",m);k++;}

if(k==5)break;

}

}