while 循环、for循环练习

1、从键盘输入一串字符,统计里面字母、数字、其他字符的个数(while循环)

#include <stdio.h>

int main()
{
   	char ch;
   	int count_01=0,count_02=0,count_03=0,count_04=0;
   	printf("亲输入一串字符进行统计\n");
   	while((ch=getchar())!='\n')
   	{
		if(ch>='a'&&ch<='z')
			count_01++;
		else if(ch>='A'&&ch<='Z')
			count_02++;
		else if(ch>='0'&&ch<='9')
			count_03++;
		else 
			count_04++;	
	}
 	printf("小写字母%d个,大写字母%d个,数字%d个,其它%d个\n",count_01,count_02,count_03,count_04);
	return 0; 
} 

2、键盘录入一个数,请判断它数能被几个9整除 。如81就可以被2个9整除

#include <stdio.h>

int cal(int num_1);
int main()
{	
	int num,count;
	printf("请输入一个数字判断能被几个9整除:");
	scanf("%d", &num);
	count=cal(num);
	printf("%d次", count);

	return 0;
}

int cal(int num_1)
{
	int ret=0;
	while(num_1%9==0)
	{
	 	ret++;
	 	num_1=num_1/9;
	} 

	return ret;
}

3、计算顾客年龄分布比例

#include <stdio.h>

int main()
{	float count_01=0,count_02=0;
	float rate_01,rate_02;
	int year_01,year_02,year_03,year_04,year_05;
	int year_06,year_07,year_08,year_09,year_10;
	printf("请输入第1位顾客年龄:\n");
	scanf("%d", &year_01);
	if(year_01>30)
		count_01++;
	else
		count_02++;
	printf("请输入第2位顾客年龄:\n");
	scanf("%d", &year_02);
	if(year_02>30)
		count_01++;
	else
		count_02++;
	printf("请输入第3位顾客年龄:\n");
	scanf("%d", &year_03);
	if(year_03>30)
		count_01++;
	else
		count_02++;
	printf("请输入第4位顾客年龄:\n");
	scanf("%d", &year_04);
	if(year_04>30)
		count_01++;
	else
		count_02++;
	printf("请输入第5位顾客年龄:\n");
	scanf("%d", &year_05);
	if(year_05>30)
		count_01++;
	else
		count_02++;
	printf("请输入第6位顾客年龄:\n");
	scanf("%d", &year_06);
	if(year_06>30)
		count_01++;
	else
		count_02++;
	printf("请输入第7位顾客年龄:\n");
	scanf("%d", &year_07);
	if(year_07>30)
		count_01++;
	else
		count_02++;
	printf("请输入第8位顾客年龄:\n");
	scanf("%d", &year_08);
	if(year_08>30)
		count_01++;
	else
		count_02++;
	printf("请输入第9位顾客年龄:\n");
	scanf("%d", &year_09);
	if(year_09>30)
		count_01++;
	else
		count_02++;
	printf("请输入第10位顾客年龄:\n");
	scanf("%d", &year_10);
	if(year_10>30)
		count_01++;
	else
		count_02++;
	rate_01=count_01*10;
	rate_02=count_02*10;
	printf("三十岁以上比例为%f%", rate_01);
	printf("三十岁以下比例为%f%", rate_02);
	
	return 0;
}

4、输入一个整数,升序显示它的所有最小因子,如120    ,输出2,2,2,3,5 

#include <stdio.h>

int main()
{	
	int num,i=2;
	printf("请输入一个整数\n");
	scanf("%d", &num);
	while(i<=num)
	{
		if(num%i==0)
		{
			printf("%d\n", i);
			num/=i;		
		}
		else
			i++	;
	}
	return 0;      
}

 5 、完全数

#include <stdio.h>

int main()
{
	int num,i;
	for(num=2;num<10000;num++)	
	{	
		int sum=0;
		for(i=1;i<num;i++)
		{
			if(num%i==0)
				sum+=i;
			else
				;
		}
		if(sum==num)
			printf("%d是一个完全数\n", num);
		else 
			;
	}
		
	return 0;
}

6、梅森素数

#include <stdio.h>
#include <math.h>

int main()
{
	int num,n,i;
	for(num=1;num<=pow(2,31);num++)
	{	
		int count=0;
		for(i=1;i<=num;i++) 
		{	
			if(num%i==0)
				count++;
			else
				;
		}
		if(count==2)
			for(n=1;n<32;n++)	
			{
				if(num==pow(2,n)-1)
					printf("%d是梅森素数\n",num);
				else
					;
			}
		else
			;
	}

	return 0;
}

7、
自由落体
假如一个小球从100米高度自由落下,每次落地后就反跳回原高度的一半。
那么求它在第10次落地时,共经过多少米?第10次反弹多高?

#include <stdio.h>

int main()
{
	int n;
	float high=100.0,sum=0;
	for(n=2;n<5;n++)
	{
			high/=2;
			sum+=2*high;
	}
	printf("共经过%f米,最后反弹%f米\n", sum+100,high/2);
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值