C语言编程设计.doc
1、求1+2+3+.+100。(循环)答案#include<stdio.h> void main() int i,sum=0; for(i=1;i<=100;i+) sum=sum+i; printf("%d",sum); 2、 求1*2*3*.*10。(循环)答案void main() int i=0,j=1; for (i=2;i<=10;i+) j*=i; printf("%d",j); return 0; 3、 输入三个数字,输出他们的最大值。(if)答案#include<stdio.h>void main() int a,b,c,d; scanf("%d,%d,%d",&a,&b,&c); d=max(a,b,c); printf("max=%d",d); getch();/*暂停看运行结果*/ int max(int x,int y,int z)int u; if(x>=y&&x>=z) u=x; else if(y>=x&&y>=z) u=y; else u=z; return(u); 4.用起泡法对十个数据排序(数组实现)答案#include<stdio.h>main ( ) int i,j,t;static int a10=5,7,4,2,3,6,1,0,9,8;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 ;for(i=0;i<10;i+)printf("%2d",ai); 5、输入十个数字,逆序输出。(数组实现)答案#include<stdio.h>main()int a10,i=0;for(i=0;i<=9;i+)scanf("%f",&ai);printf("n");for(i=9;i>=0;i-)printf("%f",ai);6输入两个数,交换他们的值并输出。(元素交换)答案#include<stdio.h>int main ()int m,n,temp;scanf("%d%d",&m,&n);if (m<n)temp=m;m=n;n=temp;printf("%d",m);return 0;7.输出9*9乘法表。(双层循环)答案#include <stdio.h>void main()int i=1;for(i; i<=9; i+)int j=1;for(j;j<=i;j+)printf("%d*%d=%d ", i, j, i*j);printf("n");8.输入一行字符,将所有的小写字母转换成大写字母,大写字母转换成小写字母,其余字符不变。输出转变后的这行字符。答案#include "stdio.h"void main()char an;int i;scanf("%s",a);printf("大写为:");for(i=0;i<=n;i+)if(ai<='z'&&ai>='a')ai=ai-32;printf("%c",ai);printf("n小写为:");for(i=0;i<=3;i+)ai=ai+32;printf("%c",ai); 9、 编写一个简单计算器程序,要求能够完成两个数的+,-,*,/四种运算。输出运算式及运算结果。(switch)6.2#include"stdio.h"main()char c;int i=0,j=0,k=0,l=0;while(c=getchar()!=n)if(c>=65&&c<=90|c>=97&&c<=122) i+;else if(c>=48&&c<=57) j+;else if(c=32) k+;else l+;printf("i=%d,j=%d,k=%d,l=%dn",i,j,k,l);6.6#include"math.h"main()int x=100,a,b,c;while(x>=100&&x<1000) a=0.01*x;b=10*(0.01*x-a);c=x-100*a-10*b;if(x=(pow(a,3)+pow(b,3)+pow(c,3) printf("%5d",x);x+;6.7main()int m,i,j,s;for(m=6;m<10000;m+)s=1;for(i=2;i<m;i+)if(m%i=0) s=s+i;if(m-s=0)printf("%5d its fastors are 1 ",m);for(j=2;j<m;j+) if(m%j=0)printf("%d ",j);printf("n");或main()int m,i,j,s;for(m=6;m<1000;m+)s=m-1;for(i=2;i<m;i+)if(m%i=0) s=s-i;if(s=0)printf("%5d its fastors are 1 ",m);for(j=2;j<m;j+) if(m%j=0)printf("%d ",j);printf("n");6.8main()int i=1,n;double t,x=1,y=2,s,sum=0;scanf("%ld",&n);while(i<=n) s=y/x;sum=sum+s;t=y;y=y+x;x=t;i+;printf("%fn",sum);11,P128 例6.10(译码)#include<stdio.h>void main()char c;while(c=getchar()!='n')c=c+4;if(c>'Z'+4|c>'z')c=c-26;printf("%c",c);printf("n");12,P111 5.5 5.6(switch)5.5#include <stdio.h>main()int x,y;printf("输入x:");scanf("%d",&x);if(x<1) y=x;printf("x=%3d, y=x=%dn",x,y);else if (x<10) y=2*x-1;printf("x=%3d, y=2*x-1=%dn",x,y);else y=3*x-11;printf("x=%3d, y=3*x-11=%dn",x,y);5.6#include <stdio.h>main() float score;char grade;case 2:printf("请输入学生成绩:");scanf("%f",&score);while(score>100|(score<0) printf("n输入有误,请重新输入:");scanf("%f",&score);switch(int)(score/10) case 10:case 9: grade=A;break;case 8: grade=B;break;case 7: grade=C;break;case 6: grade=D;break;case 5:case 4:case 3:case 1:case 0: grade=E;printf("成绩是%5.1f,相应的等级是%c。n",score,grade);13,P108 例5.5(一元二次方程求根) 例5.6(求闰年)5.5#include<stdio.h>void main()int year,leap;scanf("%d",&year);if(year%4=0)if(year%100=0)if(year%400=0)leap=1;elseleap=0;elseleap=1;elseleap=0;if(leap)printf("%d is",year);elseprintf("%d is not",year);printf("a leap year.n")5.614,P31 例2.17 例2.182.17输出50个学生中成绩高于80分者的学号和成绩2.18输出20002500年每一年是否闰年#include<stdio.h> void main() int year; year=2000; go: if(year%4 = 0)&&(year%100 != 0) | (year%400 = 0) printf("%d is run nian",year); if(year<=2500) year=year+; if(year>2500)goto end; goto go; end: getch();