Printing a number in decimal
A machine holds 42 as one value, but the screen wants the two characters 4 and 2. Splitting a number into digits is a small loop of division and remainder.
●○○ easy
The idea
To print a number, take its last digit with "remainder by 10", divide by 10, and repeat until nothing is left. The digits come out last first, so they are collected and printed in reverse.
Read the story
Each digit d is printed as the character with code 48 + d, because 48 is the code of 0.
Side by side
6 squared is 36, six more is 42.
Deadfish prints decimal by itself
6 squared is 36, six more is 42. o prints decimal, its only kind of output.
Expected output
42
Output
Befunge has .
. prints a number and a space.
Expected output
42
Output
The kernel builds it from a word
The kernel has no "print a number". The word num builds the digits as a list.
Expected output
42
Output
num => [ ] SWAP BEGIN DUP 10 MOD 48 ADD rot CAT SWAP 10 DIV DUP NOT UNTIL DROP- Start with an empty list. Take
10 MOD, add 48, put that digit in front of the list, divide by 10, until the number is 0. EMITof a list prints each element as a byte.
In brainfuck
brainfuck has no division, so it uses a known loop that divides by counting.
Divide by 10, print two digits
brainfuck has no division, so it uses a known loop that divides by counting. It starts with n = 42 and d = 10 side by side and ends with the remainder and the quotient in the next cells.
Expected output
42
Output
- Cells before,
42 10 0 0. After the loop,0 8 2 4, which is 10 minus the remainder, the remainder 2, and the quotient 4. >>>goes to the quotient. 48 plus signs make it the character 4.<and 48 more make the remainder the character 2.- This loop is from the collection of brainfuck algorithms on esolangs.org. For numbers of three digits it is run twice.