First and Last Digit
Programming Basics · Python
TASK
Problem
Given positive integer 1..999999, print its first and last digits separated by space.
EXAMPLE
Example
40782
4 2
LIMITS
Constraints
n ≥ 1
LEARN
Theory for this problem
+
`input()` reads data entered by the user. The result of `input()` is initially a string. `int()` converts a numeric string into an integer. The `//` operator performs integer division and keeps the whole-number part. The `%` operator returns the remainder after division. Comparison operators `==`, `!=`, `<`, `>`, `<=`, `>=` compare values and produce `True` or `False`. `if` checks a condition and runs its block when the condition is true. `else` runs when the corresponding `if` condition is false. `print()` displays the program result. Print only what the problem statement requires.
**Connection to this task.** The `//` and `%` operators complement each other: the first gives the integer quotient and the second gives the remainder. This is useful when splitting a number into groups or digits. The expression `first` shows how this idea is applied to the task data.
NEED HELP?
Hints
+
['Last is `% 10`; first can be selected by the number range.']
ANSWER
Solution
+
n = int(input())
last = n % 10
if n < 10:
first = n
elif n < 100:
first = n // 10
elif n < 1000:
first = n // 100
elif n < 10000:
first = n // 1000
elif n < 100000:
first = n // 10000
else:
first = n // 100000
print(first, last)