Lab 1 – Reverse Order (Print Digits in Reverse Column)
Score: 0.1
Task: Write a program that reads a natural number and prints its digits in a column in reverse order.
📌 Algorithm
- Read the integer
n. - Set up a
whileloop that runs as long asn != 0. - Inside the loop,
n % 10extracts the current last digit. print()displays it on a new line.n //= 10removes the last digit.- The loop repeats until
nbecomes 0.
💻 Solution Code
n = int(input())
while n != 0:
print(n % 10)
n = n // 10
📤 Expected Output
Input: 12345 Output: 5 4 3 2 1
📖 Step‑by‑Step Explanation
- Read the integer
n. - Set up a while loop that runs as long as
nis not zero. This ensures we process every digit, including the last one. - Inside the loop,
n % 10extracts the current last digit. print()displays it on a new line.n //= 10removes the last digit, bringing the next digit into the "last" position.- The loop repeats until
nbecomes 0.