如何获取数字的数字,而不将其转换为字符串/ char数组? [英] How to get the digits of a number without converting it to a string/ char array?

查看:90
本文介绍了如何获取数字的数字,而不将其转换为字符串/ char数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得数字的数字在C ++中而不转换为字符串或字符数组?

How do I get what the digits of a number are in C++ without converting it to strings or character arrays?

推荐答案

以下按照升序重要性(即单位,然后是十等)打印数字:

The following prints the digits in order of ascending significance (i.e. units, then tens, etc.):

do {
    int digit = n % 10;
    putchar('0' + digit);
    n /= 10;
} while (n > 0);

这篇关于如何获取数字的数字,而不将其转换为字符串/ char数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆