Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Reverse Digits of a Number

Basic Acceptance 89.81% Points 10

You are given a non-negative integer n. Using only arithmetic operations, your task is to return the number formed by reversing its digits.

The reversed number should not contain unnecessary leading zeros. For example, reversing 120 produces 21, not 021.

Examples
Example 1

Input: n = 1234
Output: 4321
Explanation: Reversing the digits of 1234 produces 4321.

Example 2

Input: n = 120
Output: 21
Explanation: Reversing 120 gives 021, which is represented as 21.

Example 3

Input: n = 5
Output: 5
Explanation: A single-digit number remains unchanged when its digits are reversed.

Constraints
  • 0 <= n <= 109
Topics
Data Structures Basics
Companies
Apple Walmart
Added 31 Oct 2026 89.81% acceptance
Ln 1, Col 1Sign in to save your code
123
321