开发者

Print an array of numbers from the least significant digit to most significant digit?

开发者 https://www.devze.com 2022-12-14 21:06 出处:网络
8 | * 7 | * 6 | * 5 | * 4 | * * 3 |* * * * * * 2 |* * * * *** ** * * 1 |* * *** ****** **** * * +---------------------------
8 | *
7 | *
6 | *
5 | *
4 | * *
3 |* * * * * *
2 |* * * * *** ** * *
1 |* * *** ****** **** * *
+---------------------------
   012345678901234567890123456
             11111111112222222

how would you print numbers from the least significant digits to the most significant digits (like the numbers shown on the x-axis)? 开发者_JS百科Thank you


Put the number in a temp.

The next digit to print is temp % 10

Divide 10 into temp.

If temp isn't 0, repeat the prior two steps.


Printing from the LSD to the MSD is actually simpler then the other way around. The reason why is that the remainder/division technique to extract the digits of a number produces the least-significant before the most-significant.

if (i == 0)
    output_digit(0)
else
    while (i != 0)
        output_digit(i % base)
        i = i / base

This will output digits in the order you want. For base 10, the number 123 will first output 3, then 2 and finally 1.

0

精彩评论

暂无评论...
验证码 换一张
取 消