C语言程序习题答案.doc
C语言程序习题答案例1.1:/* Note:Your choice is C IDE */#include "stdio.h"void main()printf("This is a C program.n");return 0;例1.2:/* Note:Your choice is C IDE */#include "stdio.h"void main()char a,b,sum;a=123;b=456;sum=a+b;printf("sum is %cn",sum);return 0;例1.3:/* Note:Your choice is C IDE */#include "stdio.h"/主函数int main()int max(int x,int y);int a,b,c;scanf("max=%d,%d",&a,&b);c=max(a,b);printf("max=%dn",c);return 0;/求两个整数中的较大者的max函数int max(int x,int y);int z;if(x>y)z=x;else z=y;return(Z);例3.1:/* Note:Your choice is C IDE */#include "stdio.h"void main()float f,c;f=64.0;c=(5.0/9)*(f-32);printf("f%fnc%fn",f,c);return 0;例3.2:/* Note:Your choice is C IDE */#include "stdio.h"void main()float p0=1000,r1=0.0036,r2=0.0225,r3=0.0198,p1,p2,p3;p1=p0*(1+r1);p2=p0*(1+r2);p3=p0*(1+r3/2)*(1+r3/2);printf("p1=%fnp2=%fnp3=%fn",p1,p2,p3);return 0;例3.3:/* Note:Your choice is C IDE */#include "stdio.h"void main()char c1,c2;c1='A'c2=c1+32;printf("%cn",c2);printf("%dn",c2);return 0;例3.4:/* Note:Your choice is C IDE */#include "stdio.h"#include "math.h"void main()double a,b,c,s,area;a=3.67;b=5.43;c=6.21;s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c);printf("a=%ftb=%ftc=%fn",a,b,c);printf("area=%fn",area);return 0;例3.5:/* Note:Your choice is C IDE */#include "stdio.h"#include"math.h"void main()double a,b,c,disc,x1,x2,p,q;scanf("%lf%lf%lf",&a,&b,&c);disc=b*b-4*a*c;p=-b/(2.0*a);q=sqrt(disc)/(2.0*a);x1=p+q;x2=p-q;printf("x1=%7.2fnx2=%7.2fn",x1,x2);return 0;例3.6:/* Note:Your choice is C IDE */#include "stdio.h"void main()double a=1.0;printf("%10.15fn",a/3);return 0;例3.7:/* Note:Your choice is C IDE */#include "stdio.h"void main()float a;a=10000/3.0;printf("%fn",a);return 0;例3.8:/* Note:Your choice is C IDE */#include "stdio.h"void main()char a='B',b='O',c='Y'putchar(a);putchar(b);putchar(c);putchar("n");return ;例3.9:/* Note:Your choice is C IDE */#include "stdio.h"void main()char a,b,c;a=getchar();b=getchar();c=getchar();putchar(a);putchar(b);putchar(c);putchar("n");return 0;例3.10:/* Note:Your choice is C IDE */#include "stdio.h"void main()char c1,c2;c1=getchar();c2=c1+32;putchar(c2);return 0;例4.1:/* Note:Your choice is C IDE */#include "stdio.h"#include "math.h"void main()double a,b,c,disc,x1,x2,p,q;scanf("%lf%lf%lf",&a,&b,&c);disc=b*b-4*a*c;if(disc<0)printf("This equation hasnit real rootsn");elsep=-b/(2.0*a);q=sqrt(disc)/(2.0*a);x1=p+q;x2=p-q;printf("real roots:nx1=%7.2fnx2=%7.2fn",x1,x2);return 0;例4.2:/* Note:Your choice is C IDE */#include "stdio.h"void main()float a,b,t;scanf("%f,%f",&a,&b);if(a>b)t=a;a=b;b=t;printf("%5.2f,%5.2fn",a,b);return 0;例4.3:/* Note:Your choice is C IDE */#include "stdio.h"void main()float a,b,c,t;scanf("%f,%f,%f",&a,&b,&c);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("%5.2f,%5.2f,%5.2fn",a,b,c);return 0;例4.4:/* Note:Your choice is C IDE */#include "stdio.h"void main()char ch;scanf("%c",&ch);ch=(ch>='A'&&ch<='Z')?(ch+32):ch;printf("%c",ch);return 0;例4.5:/* Note:Your choice is C IDE */#include "stdio.h"void main()int x,y;scanf("%d",&x);if(x>=0);if(x>0)y=1;elsey=0;elsey=-1;printf("x=%d,y=%d",x,y);return 0;习题4.8:/* Note:Your choice is C IDE */#include "stdio.h"void main()int grade;scanf("%d",&grade);if(grade>=90)printf("A");elseif(grade>=80)printf("B");elseif(grade>=70)printf("C");elseif(grade>=60)printf("D");elseprintf("E");return 0;例4.9:/* Note:Your choice is C IDE */#include "stdio.h"void main()int i,n;scanf("%d",&i); n=i/10000; i=1%10000; printf("n=%dn",n); n=i/1000; i=1%1000; printf("n=%dn",n); n=i/100; i=1%100; printf("n=%dn",n); n=i/10; i=1%10; printf("n=%dn",n); n=i; printf("n=%dn",n); return 0;/* Note:Your choice is C IDE */#include "stdio.h"void main()long n,i;scanf("%d",&i); n=i/10000; i=i%10000; printf("n=%dn",n); n=i/1000; i=i%1000; printf("n=%dn",n); n=i/100; i=i%100; printf("n=%dn",n); n=i/10; i=i%10; printf("n=%dn",n); n=i; printf("n=%dn",n); return 0;/* Note:Your choice is C IDE */#include "stdio.h"void main()long n,a,b,c,d,e;printf("Please input a numbern"); scanf("%d",&n);if(n/10000>=1)a=n/10000;n=n%10000;b=n/1000;n=n%1000;c=n/100;n=n%100;d=n/10;e=n%10;printf("This is a 5'weishu'n");printf("%dn%dn%dn%dn%dn",a,b,c,d,e);elseif(n/1000>=1)b=n/1000;n=n%1000;c=n/100;n=n%100;d=n/10;e=n%10;printf("This is a 4'weishu'n");printf("%dn%dn%dn%dn",b,c,d,e);elseif(n/100>=1)c=n/100;n=n%100;d=n/10;e=n%10;printf("This is a 3'weishu'n");printf("%dn%dn%dn",c,d,e);elseif(n/10>=1)d=n/10;e=n%10;printf("This is a 2'weishu'n"); printf("%dn%dn",d,e);elseprintf("This is a 1'weishu'n");printf("%dn",n);例5.1:/* Note:Your choice is C IDE */#include "stdio.h"void main()int i=1,sum=0;dosum=sum+i;i=i+1;while(i<=100);printf("sum=%dn",sum);return 0;例5.2:/* Note:Your choice is C IDE */#include "stdio.h"void main()int i=1,sum=0;dosum=sum+i;i=i+1;while(i<=100);printf("sum=%dn",sum);return 0;例5.3:/* Note:Your choice is C IDE */#include "stdio.h"void main()int i,sum=0;printf("please enter i,i=?");scanf("%d",&i);while(i<=10)sum=sum+i;i+;printf("sum=%dn",sum);return 0;例5.4:/* Note:Your choice is C IDE */#include "stdio.h"void main()int n;for(n=100;n<=200;n+)if(n%3=0)continue;printf("%d",n);printf("n");return 0;例5.5:/* Note:Your choice is C IDE */#include "stdio.h"void main()int i,j,n=0;for(i=1;i<=4;i+)for(j=1;j<=5;j+,i+)if(n%5=0)printf("n");printf("%dt",i*j);printf('n");return 0;例5.6:/* Note:Your choice is C IDE */#include "stdio.h"void main()int i,j,n=0;for(i=1;i<=4;i+)for(j=1;j<=5;j+,n+)if(n%5=0)printf("n");printf(" %d",i*j);printf("n");return 0;例5.7:/* Note:Your choice is C IDE */#include "stdio.h"#include "math.h"void main()int f1=1,f2=1,f3,i;printf("%dn%dn",f1,f2);for(i=1;i<=38;i+)f3=f1+f2;printf("%dn",f3);fi=f2;f2=f3;return 0;例5.8:/* Note:Your choice is C IDE */#include "stdio.h"#include "math.h"void main()int f1=1,f2=1,f3,i;printf("%dn%dn",f1,f2);for(i=1;i<=38;i+)f3=f1+f2;printf("%dn",f3);f1=f2;f2=f3; return 0;例5.9:/* Note:Your choice is C IDE */#include "stdio.h"#include "math.h"void main()int n,i;printf("please enter an integer number,n=?");scanf("%d",&n);for(i=2;i<=n-1;i+)if(n%i=0)printf("number is a sushu");break;else continue;printf("number is not a sushu");return 0;例5.10:/* Note:Your choice is C IDE */#include "stdio.h"void main()int i,n,k=0;for(n=101;n<=200;n=n+1) for(i=2;i<=n-1;i+)if(n%i=0)k=k+1;if(k>0)printf("%dn",n);break;else continue; return 0;习题5.5:/* Note:Your choice is C IDE */#include "stdio.h"void main()int a,b,n,k;double c=0,t=1;scanf("%d%d",&a,&n);b=a;for(k=1;k<=n;k+)c=a+c;t=10*t;a=a+b*t;printf("%f",c);return 0;习题5.6:/* Note:Your choice is C IDE */#include "stdio.h"void main()float n=1,k=1,s=0;while(n<=4)s=s+k;n=n+1;k=k*n;printf("%fn",s);return 0;习题5.7:/* Note:Your choice is C IDE */#include "stdio.h"void main() float k=1,s=0;while(k<=100)s=s+k;k=k+1;k=1;while(k<=50)s=s+k*k;k=k+1;k=1;while(k<=10)s=s+1/k;k=k+1;printf("s=%f",s);return 0;习题5.8:/* Note:Your choice is C IDE */#include "stdio.h"void main()int n,i,j,k;for(n=100;n<=999;n+)i=n/100;j=n%100;j=j/10;k=n%10;if(n=i*i*i+j*j*j+k*k*k)printf("%dn",n);return 0;习题5.10:/* Note:Your choice is C IDE */#include "stdio.h"void main()float i=1,j=2,k=3,s=2,n,a;for(n=2;n<=20;n+)s=s+k/j;i=j;a=k;k=j+k;j=a;printf("%fn",s);return 0;习题5.16:/* Note:Your choice is C IDE */#include "stdio.h"void main()int i,j,k;for(i=0;i<=3;i+)for(j=0;j<=2-i;j+)printf(" ");for(k=0;k<=2*i;k+)printf("*");printf("n");for(i=0;i<=2;i+)for(j=0;j<=i;j+) printf(" ");for(k=0;k<=4-2*i;k+)printf("*");printf("n");return 0;例6.1:/* Note:Your choice is C IDE */#include "stdio.h"void main()int i,a10;for(i=0;i<=9;i+)ai=i;for(i=9;i>=0;i-)printf("%d ",ai);printf("n");return 0;例6.2:/* Note:Your choice is C IDE */#include "stdio.h"void main()int i;int f20=1,1;for(i=2;i<20;i+)fi=fi-1+fi-2;for(i=0;i<20;i+)if(i%5=0)printf("n");printf("%12d",fi);printf("n");return 0;例6.3:/* Note:Your choice is C IDE */#include "stdio.h"void main()int a10;int i,j,t;printf("input 10 number :n");for(i=0;i<10;i+)scanf("%d",&ai);printf("n");for(j=0;j<9;j+)for(i=0;i<9-j;i+)if(ai>ai+1)t=ai;ai=ai+1;ai+1=t;printf("the sorted numbers :n");for(i=0;i<10;i+)printf("%d ",ai);printf("n");return 0;例7.1:/* Note:Your choice is C IDE */#include "stdio.h"void main()void print_star();void print_message();print_star();print_message();print_star();return 0;void print_star()printf("*n");void print_message()printf("How do you do!n");例7.2:/* Note:Your choice is C IDE */#include "stdio.h"void main()int max(int x,int y);int a,b,c;printf("please enter two integer numbers:");scanf("%d,%d",&a,&b);c=max(a,b);printf("max is %dn",c);return 0;int max(int x,int y)int z;z=x>y? x:y;return (z);例7.3:/* Note:Your choice is C IDE */#include "stdio.h"void main()float add(float x,float y);float a,b,c;printf("Please enter a and b:");scanf("%f,%f",&a,&b);c=add(a,b);printf("sum is %fn",c);return 0;float add(float x,float y)float z;z=x+y;return (z);数据转换:/* Note:Your choice is C IDE */#include "stdio.h"void main()char a,b,c,d,e,f;int g=22,sum;a='n'b='a'c='n'd='n'e='a'f='n'sum=a+b+c+d+e+f+g;printf("sum is %dn",sum);return 0;