Educatifu
Open menu

Reverse Integer

Easymath

Reverse the digits of an integer, keeping its sign. Leading zeros in the result are dropped (e.g. 120 → 21).

Input

A single integer (it may be negative).

Output

The integer with its digits reversed.

Examples

Example 1

Input
123
Output
321

123 → 321.

Example 2

Input
-45
Output
-54

Sign kept: -45 → -54.

Example 3

Input
120
Output
21

Trailing zero becomes leading and is dropped: 120 → 21.

Constraints

  • -10^15 <= n <= 10^15

Hints

Reveal hint 1

Handle the sign separately, then reverse the digit characters.

Reveal hint 2

Converting the reversed digit string back to an integer naturally drops leading zeros.

Reference solution (spoiler — try it yourself first)

Approach

Split off the sign, reverse the digit characters of the absolute value, parse back to an integer, and reapply the sign.

s = input().strip()
sign = -1 if s[0] == '-' else 1
digits = s.lstrip('-')
print(sign * int(digits[::-1]))

← All practice problems

Bring us the problem, not a perfect specification

Tell us what needs to change, who it affects and any important deadline. We will review the context and reply with useful next questions.

  1. 01Share contextDescribe the workflow, constraint or risk.
  2. 02Clarify togetherWe identify missing facts and useful options.
  3. 03Choose a startAgree a focused assessment or delivery step.
Start a conversation