周末作业7.18

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

#include <stdio.h>

int main()
{
    int count1=0,count2=0,count3=0;
    char ch;
    printf("请输入一串字符\n");
    while((ch=getchar())!='\n')
    {
        if((('a'<=ch)&&(ch<='z'))||(('A'<=ch)&&(ch<='Z')))
            count1++;
        else if(('0'<=ch)&&(ch<='9'))
            count2++;
        else
            count3++;
    }
        printf("字母有%d个\n",count1);
        printf("数字有%d个\n",count2);
        printf("其他字符有%d个\n",count3);
    
    return 0;
}

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

#include <stdio.h>

int main()
{
    int num,a=0;
    int times=1;
        printf("请输入一个整数来判断是否能被9整除\n");
        scanf("%d",&num);
    if(num>=9)
    {
    while(num>=9)
        {
            num=num%9;    
            times++;
            if(num==0)
            printf("这个数可以被%d个9整除。\n",times);
        }
        

    }
        else
        printf("输入的数必须大于9"); 
    return 0; 
}

3. 计算顾客比例

#include<stdio.h>

int main()
{
    int age=0;
    float count1,count2;
    printf("请输入顾客的年龄\n");
    while(count1<5) 
    {
        scanf("%d",&age);
        if(age<30)
            count1++;
        else
            count2++;
    }
    printf("三十岁以下的比例为%.1f%%",(count1/(count1+count2))*100);
    printf("三十岁以上的比例为%.1f%%",(count2/(count1+count2))*100);
    return 0;
}

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

 

#include <stdio.h>

int main()
{
    int num,a=2;
    printf("请输入你要求最小因子的数\n");
    scanf("%d",&num);
    while(num>=a)
    {
        if(num%a==0)
        {
            num=num/a;
            printf("%d",a);
        }
    
        else 
            a++;
        
    }
    
    return 0;
}

5.完全数(for循环) 

#include <stdio.h>

int main()
{
    int num,a,b,c=0,d=0;
    printf("请输入一个数判断这个数是否为完全数\n");
    scanf("%d",&num);
    for(a=1;a<=num;a++)
    {
        if(num%a==0)
        {
            b=num/a;
            c=a+b+c;
        }
        else
            ;
            
    }
    if(num==(c/4))
        printf("%d是一个完全数",num);
    else
        printf("%d不是一个完全数",num);
    return 0;
}

 

6.  为完成

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

#include <stdio.h>
 
int main()
{
    int n;
    float high=100.0,sum=0;
    for(n=1;n<10;n++)
    {
            high/=2;
            sum+=2*high;
    }
    printf("小球共经过%.1f米,最后反弹了%.3f米\n", sum+100,high/2); 
    return 0;
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值