当前位置:文档之家› C语言程序设计教程课后习题答案

C语言程序设计教程课后习题答案

main()
{ int i=0;
float s=0.0;
while(2*i+1<=101)
{ s=s+pow(-1,i)/float(2*i+1);
i=i+1;
}
printf("s= %f\n",s);
}
5
#include "stdio.h"
#include"math.h"
main()
{ int i,j,k,n=0;
main()
{
float money;
int year;
printf("\nplease input the money and the year:");
scanf("%f%d",&money,&year);
if(year==1) money+=money*0.00315*12*year;
if(year==2) money+=money*0.00330*12*year;
else printf("no\n");
}
7.
#include <stdio.h>
main()
{
int year,month;
printf("please input the year and month:");
scanf("%d%d",&year,&month);
switch(month)
{
case 1:
}
6
#include "stdio.h"
main()
{ int n,i,j;
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
printf("*");
printf("\n");
}
}
7
#include "stdio.h"
case 2:if(year%4==0&&year%100!=0||year%400==0)
{
printf("this month have 29 days.");
break;
}
else
{
printf("this month have 28 days.");
break;
}
}
}
8.
#include <stdio.h>
}
第3章
1
#include "stdio.h"
main()
{ float x[10],sum=0;
int i=0;
printf("please input 10 numbers(-10e6<x<10e6)\n");
while(i<10)
{ scanf("%f",&x[i]);
sum=sum+1/x[i];
printf("the number is:%d\n",i*1000+j*100+k*10+m);
}
第4章
1-5 ABDCB 6-10 BCBCA 11-13 ABA
14
#include "stdio.h"
main()
{
float a1,b1,a2,b2,re1,im1,re2,im2,re3,im3;
re2=a1-a2;
im2=b1-b2;
printf("(%f+j%f)-(%f+j%f)=%f+j%f\n",a1,b1,a2,b2,re2,im2);
re3=a1*a2-b1*b2;
im3=a1*b2+a2*b1;
printf("(%f+j%f)*(%f+j%f)=%f+j%f\n",a1,b1,a2,b2,re3,im3);
while(x[i]!=0)
{ sum=sum+x[i];
i=i+1;
scanf("%d",&x[i]);
}
printf("the sum is %d\n",sum);
printf("the average is %.2f\n",float(sum)/i);
}
3
#include "stdio.h"
#include "math.h"
}
第5章
1-5 CACCD6-7 DA
8
#include "stdio.h"
main()
{
char str[100];
int num1=0,num2=0,i=0;
scanf("%s",str);
while(str[i]!='\0')
{
if(str[i]>='A'&&str[i]<='Z'||str[i]>='a'&&str[i]<='z')
num1++;
else if(str[i]>='0'&&str[i]<='9')
num2++;
i++;
}
printf("num1=%d,num2=%d",num1,num2);
}
9笔者在程序中直接给数组元素赋了值,读者可以修改为用scanf函数读入数值;另外,还可以用第7章宏定义的方法设定数组的长度,从而实现对元素个数不同的数组进行排序。
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:printf("this month have 31 days.");
break;
case 4:
case 6:
case 9:
case 11:printf("this month have 30 days.");
break;
printf("Please input the first number:");
scanf("%f %f",&a1,&b1);
printf("Please input the second number:");
scanf("%f %f",&a2,&b2);
re1=a1+a2;
im1=b1+b2;
printf("(%f+j%f)+(%f+j%f)=%f+j%f\n",a1,b1,a2,b2,re1,im1);
i=i+1;
}
printf("the sum of 10 numbers is %.2f\n",sum);
}
2
#include "stdio.h"
main()
{ int x[100],sum=0,aver,i=0;
printf("please input numbers until 0\n");
scanf("%d",&x[i]);
#include "stdio.h"
main()
{
int a[5]={2,4,1,3,0},i,j,temp;
for(i=0;i<=4;i++)
printf("%d,",a[i]);
printf("\n");
for(j=1;j<=4;j++)
for(i=j;i<=4;i++)
if(a[i]<a[j-1])
main()
{ int i=1,j,f;
long int s=0;
while(i<=10)
{ for(f=1,j=1;j<=i;j++)
f=f*j;
s=s+pow(-1,i-1)*f;
i=i+1;
}
printf("s= %ld\n",s);
}
4
#include "stdio.h"
#include "math.h"
return(1);
}
for(i=2;i<=n/2;i++)
{ for(j=2;j<i;j++)
if(i/j*j==i)
break;
if(j==i)
{for(k=2;k<n-i;k++)
if((n-i)/k*k==(n-i))
break;
if(k==n-i)
printf("%ld=%d+%d\n",n,i,n-i);
10+4+2=16
20+4+3=27
相关主题