一
1.1
#include <stdio.h> int main(){ int i,sum=0; i = 1; while(i <= 100) { sum += i; i++; } printf("sum = %d ",sum); return 0; }
1.2
#include <stdio.h> int main(){ int i,sum = 0; i = 1; sum = 0; do{ sum+=i; i++; }while(i <= 100); printf("sum = %d ",sum); return 0;
1.3
#include <stdio.h> int main(){ int i,sum=0; sum=0; for(i = 1;i <= 100;i++) sum += i; printf("sum = %d ",sum); return 0; }
二
2.1
#include <stdio.h> int main(){ int i,sum = 1,n; i=1; printf("请输入n的值:"); scanf("%d",&n); while(i <= n) { sum = sum*i; i++; } printf("sum = %d ",sum); return 0; }
2.2
#include <stdio.h> int main(){ int i,sum = 1,n; i=1; printf("请输入n的值:"); scanf("%d",&n); do{ sum = sum*i; i++; }while(i <= n); printf("sum = %d ",sum); return 0; }
2.3
#include <stdio.h> int main(){ int i,sum = 1,n; printf("请输入n的值:"); scanf("%d",&n); for(i = 1;i <= n;i++) sum=sum*i; printf("sum = %d ",sum); return 0; }
三
#include <stdio.h> #include <math.h> int main(){ long int k,sum=1,sum1,sum2; do{ sum+=k; k++; }while(k <= 100); for(k = 1;k< = 50;k++) sum1=k*k; for(k =1/k; printf("总数为:%d ",sum+sum1+sum2); return 0; }
四
#include <stdio.h> int main(){ char c; int zm = 0,sz = 0,kg = 0,qt = 0; while((c=getchar())!=' '){ if((c>='a' && c<='z') ||(c>='A' && c<='Z')) zm++; if(c==' ') kg++; if(c>='0' &&c<='9') sz++; else qt++; } printf("字母个数:%d ",zm); printf("空格个数:%d ",kg); printf("数字个数:%d ",sz); printf("其他字符个数:%d ",qt); }
五
#include<stdio.h> int main(){ int a,b,m,n; printf("请输入正整数m和n: "); scanf("%d%d",&a,&b); m=a; n=b; while(a!=b) if(a>b) a=a-b; else b=b-a; printf("最大公约数为:%d ",a); printf("最小公倍数为:%d ",m*n/a); }