动画介绍:while(表达式) 循环体语句 do 循环体语句 while(表达式) for(表达式1;表达式2;表达式3) 循环体语句 break退出循环 continue结束本次循环,回头再循环 1)求10到40之间的所有素数 #include "stdio.h" main() { int a,b,n; for(a=10;a<41;a++) { n=1; for(b=2;b<8;b++) if(a%b==0){n=0;break;} if(n==1)printf("%4d",a); } } 2)求两个整数的最大公约数最小公倍数 #include "stdio.h" main() { int a,b,x,y,t; scanf("%d%d",&x,&y); a=x;b=y; t=a%b; while(t!=0) {a=b;b=t;t=a%b;} printf("x=%d,y=%d,最大公约数是%d,最小公倍数是%d\n",x,y,b,x*y/b); } 3)在键盘上输入若干字符,把小写转换为大写,其他字符不变 #include "stdio.h" main() { char ch; ch=getchar(); while(ch!=’#’) { ch=ch>=’a’&&ch<=’z’?ch-32:ch; putchar(ch); ch=getchar(); } }
| |