32.2k views
5 votes
Write a program that inputs a non negative integer,separates the integer into its digits and prints them separated by tabs each.For example, if the user types in 42339, the program should print:42339

User Ramsey
by
5.1k points

1 Answer

6 votes

Answer:

tab = ""

number = int(input("Enter a nonnegative integer: "))

while number:

digit = number % 10

if len(str(number)) != 1:

tab += str(digit) + "\t"

else:

tab += str(digit)

number //= 10

print(tab[::-1])

Step-by-step explanation:

* The code is in Python

- Initialize an empty string to hold the digits

- Ask the user for the input

Inside the loop:

- Get the digits of the number. If the length of the number is not 1 (If it is not the first digit), put a tab between the digits. Otherwise, just put the number (This will get the numbers from the last digit. If number is 123, it gets 3 first, then 2, then 1)

- Print the string in reverse order