c语言之反向输出四位数

1.0版.将个,十,百,千都表示出来

#include <stdio.h>
int main()
{
    int input = 0;
    scanf("%d",&input);
    int a=input/1000;
    int b=input%1000/100;
    int c=input%1000%100/10;
    int d=input%1000%100%10;
    int output = a+b*10+c*100+d*1000;
    printf("%d",output);
}

在这里遇到问题 输入1000,正确输出结果应为0001,实际输出为1,考虑不完善

2.0版.

用while写

#include <stdio.h>
int main()
{
   int input = 0;
    int output=0;
    scanf("%d",&input);
    while(input)
    {
        output=input%10+output*10;
        input/=10;
    }
    printf("%d",output);
}

遇到了1.0版相同的错误

 3.0版,用while和数组写

#include <stdio.h>
int main()
{
   int input = 0;
    int k =0;
    int arr[50];
    scanf("%d",&input);
    while(input)
    {
        arr[k++]=input%10;
        input/=10;
    }
    for(int i =0;i<k;i++)
    {
        printf("%d",arr[i]);
    }
}

注意点:

1.k++最后一次是5,for循环内应i<k或者i<=k-1

2.while循环中的input应该改变,否则出现死循环

3,数组的[ ]内不能是变量

4.0版

#include <stdio.h>
int main()
{
  int n;
  scanf("%d",&n);
  while(n)
  {
      printf("%d",n%10);
      n/=10;
  }
  return 0;
}

 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值