首页 > 代码库 > 实验3 循环结构程序设计

实验3 循环结构程序设计

#include<stdio.h>void main(){	int i,sum=0;	//方法一:	i=1;	while(i<=100){		sum+=i;		i++;	}	printf("sum = %d\n",sum);		//方法二:	i=1;	sum = 0;	do{		sum+= i;	} while (i<=100);    printf("sum = %d\n",sum);        //方法三:	sum=0;	for(i=1;i<=100;i++)		sum+= i;		printf("sum = %d\n",sum);		 }

技术分享

 

 

2.

#include<stdio.h>

int main(){

      int n,i,jiec=1;

      printf("请输入一个数字n: ");

      scanf("%d",&n);

      i=n;

      while(i>=1){

           jiec = jiec * i;

           i--;

      }

      printf("%d!=%d\n",n,jiec);

      return 0;

}

 

技术分享

 

3.

#include<stdio.h>int main(){	int k,sum = 0;	for(k=1;k<=100;k++)		sum+=k;	printf("sum=%d\n",sum);		for(k=1;k<=50;k++)		sum=sum + k * k;	printf("sum=%d\n",sum);		for(k=1;k<=10;k++)		sum=sum + 1/k;	printf("%d\n",sum);	return 0;}

 技术分享

 

4.

#include<stdio.h>int main(){	char c;	int zm=0,sz=0,kg=0,qt=0;	while((c=getchar())!=‘\n‘){		if(c>=‘a‘ &&c<=‘z‘ || c>=‘A‘&&c<=‘z‘)			zm++;		else if(c==‘ ‘)			kg++;		else if(c>=‘0‘ && c<=‘9‘)			sz++;		else			qt++;	}	printf("字母个数:%d",zm);	printf("数字个数:%d",sz);	printf("空格个数:%d",kg);	printf("其他个数:%d",qt);	return 0;}

 技术分享

 

5.

#include<stdio.h>int main(){	int m,n,t,i,gys=1,gbs;	printf("请输入两个数字:");	scanf("%d%d",&m,&n);	if(n>m){		t=m;		m=n;		n=t;	}	for(i=n;i>=1;n%i==0)		break;		printf("m=%d,n=%d",m,n);	if(i!=0){		gys=i;		printf("最大公约数为:%d\n",gys);	}		gbs=m*n/gys;		printf("最大公倍数为:%d\n",gbs);		return 0;}

 技术分享

 

6.

#include<stdio.h>int main(){	int n,k1,k2,k3;	for(n=100;n<=999;n++){		k1 = n % 10;		k2 = n / 10 % 10;		k3 = n / 100;		if(n==k1*k1*k1+k2*k2*k2+k3*k3*k3)			printf("%d\n",n);	}	return 0;}

 技术分享

实验3 循环结构程序设计