0
0
CsharpProgramBeginner · 2 min read

C# Program to Reverse Number Using While Loop

You can reverse a number in C# using a while loop by extracting the last digit with num % 10, adding it to a reversed number multiplied by 10, and then removing the last digit with num /= 10 until num becomes zero.
📋

Examples

Input1234
Output4321
Input1000
Output1
Input7
Output7
🧠

How to Think About It

To reverse a number, repeatedly take its last digit using the remainder operator %, add that digit to a new number shifted left by one digit (multiply by 10), and then remove the last digit from the original number by dividing by 10. Repeat this until the original number is zero.
📐

Algorithm

1
Get the input number.
2
Initialize reversed number to zero.
3
While the input number is greater than zero:
4
Extract the last digit using modulus 10.
5
Multiply 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

csharp
using System;
class Program {
    static void Main() {
        int num = 1234, reversed = 0;
        while (num > 0) {
            int digit = num % 10;
            reversed = reversed * 10 + digit;
            num /= 10;
        }
        Console.WriteLine(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 is now 0, loop stops, 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.

Step 2: Build reversed number

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

Step 3: Remove last digit

Divide the original number by 10 using integer division to remove the last digit.

🔄

Alternative Approaches

Using string conversion
csharp
using System;
class Program {
    static void Main() {
        int num = 1234;
        char[] digits = num.ToString().ToCharArray();
        Array.Reverse(digits);
        string reversedStr = new string(digits);
        int reversed = int.Parse(reversedStr);
        Console.WriteLine(reversed);
    }
}
This method is simpler but uses extra memory for string conversion and is less efficient than the while loop.
Using recursion
csharp
using System;
class Program {
    static int Reverse(int num, int reversed = 0) {
        if (num == 0) return reversed;
        return Reverse(num / 10, reversed * 10 + num % 10);
    }
    static void Main() {
        int num = 1234;
        Console.WriteLine(Reverse(num));
    }
}
Recursion is elegant but can cause stack overflow for very large numbers and is less intuitive for beginners.

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

Time Complexity

The loop runs once for each digit in the number, so time complexity is O(d), where d is the number of digits.

Space Complexity

Only a few integer variables are used, so space complexity is O(1), constant space.

Which Approach is Fastest?

The while loop method is fastest and most memory efficient compared to string conversion or recursion.

ApproachTimeSpaceBest For
While loopO(d)O(1)Efficient and simple for numeric reversal
String conversionO(d)O(d)Quick coding but uses extra memory
RecursionO(d)O(d)Elegant but less efficient and risk of stack overflow
💡
Always use integer division /= 10 to remove the last digit when reversing numbers.
⚠️
Forgetting to multiply the reversed number by 10 before adding the new digit causes incorrect results.