fb pixel

Digits

While loops are very convenient when dealing with the digits of a number. If we have an integer, say 1486743242498701, we can take each of the digits one by one starting from the end, and process it. Every time we can take the remainder when dividing by 10 and then actually divide the number by 10. This will remove the last digit of the number:
n = 1486743242498701
while n != 0:
	print('The last digit:', n % 10, end=' => ')
	n //= 10
	print('Resulting n:', n)
The last digit: 1 => Resulting n: 148674324249870
The last digit: 0 => Resulting n: 14867432424987
The last digit: 7 => Resulting n: 1486743242498
The last digit: 8 => Resulting n: 148674324249
The last digit: 9 => Resulting n: 14867432424
The last digit: 4 => Resulting n: 1486743242
The last digit: 2 => Resulting n: 148674324
The last digit: 4 => Resulting n: 14867432
The last digit: 2 => Resulting n: 1486743
The last digit: 3 => Resulting n: 148674
The last digit: 4 => Resulting n: 14867
The last digit: 7 => Resulting n: 1486
The last digit: 6 => Resulting n: 148
The last digit: 8 => Resulting n: 14
The last digit: 4 => Resulting n: 1
The last digit: 1 => Resulting n: 0

Challenge

Given an integer, your task is to print its digits separated by a space.
Input
Output
123
1 2 3
36
3 6
8
8
 
To check your solution you need to sign in
Sign in to continue