Skills101
← All coding problems

Reverse the Digits

easyPlacement Coding+10 XPTime limit 2s

For each integer, print it with its digits in reverse order. Keep the minus sign of a negative number, and drop leading zeros that appear after reversing: -450 becomes -54, and 1200 becomes 21.

Input:

  • line 1: q — how many numbers
  • next q lines: one integer n each

Output: q lines — each number reversed.

Constraints: 1 ≤ q ≤ 10⁴, −10¹⁸ ≤ n ≤ 10¹⁸ (use long in Java, long long in C++).

Think about it: take the last digit with n % 10, remove it with n / 10, and build the answer as rev = rev * 10 + digit. Work with the absolute value and put the sign back at the end.

Sample input 1

3
123
-450
7

Sample output 1

321
-54
7

Sample input 2

2
1200
0

Sample output 2

21
0

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.