资源描述:
《c语言循环实验答案》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、/*九九乘法*/#includeintmain(void){inti,j;i=1;j=1;for(i=1;i<=9;i++){for(j=1;j<=9;j++)printf("%d*%d=%d",i,j,i*j);printf("");}return0;}/*求n!*/#includeintmain(void){intn,i=1;intm;m=1;printf("请输入一个正整数:");scanf("%d",&n);while(i<=n){m=m*i;i++;}printf("%d!=%d",n,m);return0;}/*求解:n!,n
2、的值由用户输入*//*思考:如何控制n变量的输入,如n输入的是负数,怎么办,溢出怎么办?*/#include"stdio.h"voidmain(){inti,fac=1,iSum=0,n;printf("请输入自然数n:");scanf("%d",&n);for(i=1;i<=n;i++){fac=fac*i;iSum=iSum+fac;}printf("1!+2!+3!+...+%d!=%d",n,iSum);}/*求解:1-n的奇数和*//*思考:1-n的偶数和怎么做?*/#include"stdio.h"voidmain(){intn,i,sum;sum=0;prin
3、tf("请输入自然数n:");scanf("%d",&n);for(i=1;i<=n;i++){if(i%2==0)sum=sum+i;}printf("前n项奇数和为:%d",sum);}/*求解:100-200之间的素数,而且每输出10个为一行。*/#include"stdio.h"#include"math.h"voidmain(){intm,k,i,n=0;for(m=100;m<=200;m++){k=sqrt(m);for(i=2;i<=k;i++)if(m%i==0)break;if(i>=k+1){printf("%d",m);n++;}if(n%10==0)p
4、rintf("");}printf("");}/*求解:n-m之间的素数,而且每输出10个为一行。*/#include"stdio.h"#include"math.h"voidmain(){intn,m,i,j=0;scanf("%d%d",&n,&m);for(n;n<=m;n++){for(i=2;i<=sqrt(n);i++)if(n%i==0)break;if(i>sqrt(n)){printf("%d",n);j=j+1;if(j%10==0)printf("");}}}/*正、倒三角*/#includeintmain(void){inti,
5、j;for(i=1;i<=4;i++){for(j=1;j<=4-i;j++)printf("");for(j=1;j<=2*i-1;j++)printf("*");printf("");}for(i=4;i>=1;i--){for(j=1;j<=4-i;j++)printf("");for(j=1;j<=2*i-1;j++)printf("*");printf("");}return0;}