0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Reverse a Number with Output

To reverse a number in Kotlin, use a loop to extract digits with % 10 and build the reversed number by multiplying the result by 10 and adding the digit; for example: while (num != 0) { reversed = reversed * 10 + num % 10; num /= 10 }.
📋

Examples

Input1234
Output4321
Input1000
Output1
Input0
Output0
🧠

How to Think About It

To reverse a number, think of peeling off the last digit repeatedly using the remainder operator %. Then, add that digit to a new number that grows by shifting digits left (multiplying by 10). Repeat until the original number is fully processed.
📐

Algorithm

1
Get the input number.
2
Initialize a variable to store the reversed number as 0.
3
While the input number is not zero:
4
Extract the last digit using modulus 10.
5
Multiply the reversed number by 10 and add the extracted digit.
6
Remove the last digit from the input number by dividing by 10.
7
Return the reversed number.
💻

Code

kotlin
fun main() {
    var num = 1234
    var reversed = 0
    while (num != 0) {
        val digit = num % 10
        reversed = reversed * 10 + digit
        num /= 10
    }
    println(reversed)
}
Output
4321
🔍

Dry Run

Let's trace the number 1234 through the code to see how it reverses.

1

Initial values

num = 1234, reversed = 0

2

First iteration

digit = 1234 % 10 = 4; reversed = 0 * 10 + 4 = 4; num = 1234 / 10 = 123

3

Second iteration

digit = 123 % 10 = 3; reversed = 4 * 10 + 3 = 43; num = 123 / 10 = 12

4

Third iteration

digit = 12 % 10 = 2; reversed = 43 * 10 + 2 = 432; num = 12 / 10 = 1

5

Fourth iteration

digit = 1 % 10 = 1; reversed = 432 * 10 + 1 = 4321; num = 1 / 10 = 0

6

Loop ends

num = 0, reversed = 4321

numdigitreversed
123444
123343
122432
114321
💡

Why This Works

Step 1: Extract last digit

Using num % 10 gets the last digit of the number, like peeling one digit off.

Step 2: Build reversed number

Multiply the current reversed number by 10 to shift digits left, then add the new digit.

Step 3: Remove last digit

Divide the original number by 10 using integer division to drop the last digit and continue.

🔄

Alternative Approaches

String conversion
kotlin
fun main() {
    val num = 1234
    val reversed = num.toString().reversed().toInt()
    println(reversed)
}
This method is simpler but uses extra memory for string conversion and may be slower for very large numbers.
Recursive approach
kotlin
fun reverseNumber(num: Int, reversed: Int = 0): Int {
    return if (num == 0) reversed else reverseNumber(num / 10, reversed * 10 + num % 10)
}

fun main() {
    println(reverseNumber(1234))
}
Uses recursion to reverse the number, elegant but may cause stack overflow for very large inputs.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once per digit in the number, so time grows linearly with the number of digits.

Space Complexity

Only a few variables are used, so space is constant regardless of input size.

Which Approach is Fastest?

The loop method is fastest and uses least memory; string conversion is simpler but slower and uses more memory; recursion is elegant but less efficient.

ApproachTimeSpaceBest For
Loop methodO(d)O(1)Efficient and memory-friendly
String conversionO(d)O(d)Quick coding, small inputs
Recursive methodO(d)O(d)Elegant code, small inputs
💡
Always use integer division / to remove digits, not floating-point division.
⚠️
Beginners often forget to update the original number inside the loop, causing infinite loops.