【C语言】求所输入整数的位数、逆序数

给一个不多于5位的正整数,要求:
1、求出它是几位数;
2、分别输出每一位数字
3、按逆序输出各位数字,例如原数为321,应输出123;

#include <stdio.h>              //包含标准库的信息
#include <math.h>               //<math.h> 头文件中声明了一些数学函数和宏
int main(void)                  //每个程序都从main函数的起点开始执行
{
    int num,indiv,ten,hundred,thousand,ten_thousand,place;//数,个,十,百,千,万,位数
    printf("请输入一个整数(0-99999)");
    scanf("%d",&num);
    if(num >9999)//大于9999且小于99999的数自然是5位数
        place = 5;
    else if (num >999)
        place = 4;
    else if (num > 99)
        place = 3;
    else if (num > 9)
        place = 2;
    else place = 1;
    printf("位数:%d\n",place);
    printf("每位数字为:");
    ten_thousand = num/10000;//取万
    thousand = (int)(num - ten_thousand * 10000)/1000;//把万减了,int是向下取整,得千位
    hundred = (int)(num - ten_thousand * 10000 - thousand * 1000)/100;//同理
    ten=(int)(num - ten_thousand * 10000 - thousand * 1000 - hundred * 100)/10;//同理
    indiv=(int)(num - ten_thousand * 10000 - thousand * 1000 - hundred * 100 - ten * 10);//同理
    switch (place)
    {
        case 5:
            printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv);//把每位数显示出来
            printf("\n反序数字为:");
            printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand);//重新组合为倒序数
            break;
        case 4:
            printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);
            printf("\n反序数字为:");
            printf("%d%d%d%d\n",indiv,ten,hundred,thousand);
            break;
        case 3:
            printf("%d,%d,%d",hundred,ten,indiv);
            printf("\n反序数字为:");
            printf("%d%d%d\n",indiv,ten,hundred);
            break;
        case 2:
            printf("%d,%d",ten,indiv);
            printf("\n反序数字为:");
            printf("%d%d\n",indiv,ten);
            break;
        case 1:
            printf("%d",indiv);
            printf("\n反序数字为:");
            printf("%d\n",indiv);
            break;
    }
    return 0;
}

运行结果
在这里插入图片描述

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lesin X

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值