Reversing numbers is a common task in programming. In this blog, we’ll explore various methods to reverse number in C#. We’ll cover four approaches: using a while loop, a function, recursion, and arrays. Each method offers a unique way to achieve the same result, providing insight into different programming techniques.

Table of Contents

**Reverse Number in C# Using While Loop**

//reverse number in C# using while loop// using System; class Program { static void Main(string[] args) { int number = 12345; int reversedNumber = 0; while (number > 0) { int digit = number % 10; reversedNumber = (reversedNumber * 10) + digit; number /= 10; } Console.WriteLine("Reversed number: " + reversedNumber); } }

**Explanation of the code:**This C# code reverses a given number using a while loop. Here’s a step-by-step explanation:

1. Initialization: We start by initializing the original number (12345) and the variable to store the reversed number.

2. While Loop: The while loop continues as long as the original number is greater than 0.

3. Digit Extraction: In each iteration, we extract the last digit of the original number using the modulo operator (%).

4. Appending Digit: We append the extracted digit to the reversed number, after multiplying it by 10 to shift its position.

5. Update Original Number: Finally, we update the original number by removing the last digit using integer division (/).

#### Output:

`Reversed number: 54321`

**Reverse Number in C# Using Function**

//reverse number in C# using function// using System; class Program { static int ReverseNumber(int num) { int reversedNumber = 0; while (num > 0) { int digit = num % 10; reversedNumber = (reversedNumber * 10) + digit; num /= 10; } return reversedNumber; } static void Main(string[] args) { int number = 2468; int reversedNumber = ReverseNumber(number); Console.WriteLine("Reversed number: " + reversedNumber); } }

**Explanation of the code:**

This C# code implements a function to reverse a given number. Here’s a step-by-step explanation:

1. Function Definition: We define a function named `ReverseNumber` that takes an integer `num` as input and returns an integer.

2. Initialization: Inside the function, we initialize variables to store the reversed number and iterate through the digits using a while loop.

3. Digit Extraction: In each iteration, we extract the last digit of the number using the modulo operator (%).

4. Appending Digit: We append the extracted digit to the reversed number, after multiplying it by 10 to shift its position.

5. Update Number: We update the original number by removing the last digit using integer division (/).

6. Function Call: In the `Main` method, we call the `ReverseNumber` function with a sample number (2468) and display the reversed number using `Console.WriteLine()`.

#### Output:

`Reversed number: 8642`

**Reverse Number in C# Using Recursion**

//reverse number in C# using reversion// using System; class Program { static int ReverseNumber(int num) { if (num == 0) return 0; int digit = num % 10; return (digit * (int)Math.Pow(10, (int)Math.Log10(num))) + ReverseNumber(num / 10); } static void Main(string[] args) { int number = 123456; int reversedNumber = ReverseNumber(number); Console.WriteLine("Reversed number: " + reversedNumber); } }

**Explanation of the code:**

This C# code reverses a given number using recursion. Here’s a breakdown:

1. Function Definition: We define a recursive function named `ReverseNumber` that takes an integer `num` as input and returns an integer.

2. Base Case: If the input number `num` is 0, indicating that all digits have been processed, the function returns 0.

3. Digit Extraction: We extract the last digit of the number using the modulo operator (%) and store it in the variable `digit`.

4. Recursive Call: We recursively call the `ReverseNumber` function with the remaining digits (num / 10) until the base case is reached.

5. Reconstructing Reversed Number: In each recursive call, we reconstruct the reversed number by multiplying the extracted digit by 10 raised to the power of the number of digits in the original number minus one (using `Math.Log10(num)`), and adding it to the reversed number from the recursive call.

6.Function Call: In the `Main` method, we call the `ReverseNumber` function with a sample number (123456) and display the reversed number using `Console.WriteLine()`.

#### Output:

`Reversed number: 654321`

**Reverse Number in C# Using Arrays**

//reverse number in C# using array// using System; using System.Linq; class Program { static void Main(string[] args) { int number = 13579; int[] digits = number.ToString().Select(c => int.Parse(c.ToString())).ToArray(); Array.Reverse(digits); int reversedNumber = int.Parse(string.Join("", digits)); Console.WriteLine("Reversed number: " + reversedNumber); } }

**Explanation of the code:**

This C# code reverses a given number using an array. Here’s how it works:

1. Converting Number to Array of Digits: The input number is converted to a string using `ToString()`. Then, LINQ’s `Select()` method is used to convert each character (digit) to an integer, and the result is stored in an array named `digits`.

2. Reversing the Array: The `Array.Reverse()` method is used to reverse the order of elements in the `digits` array.

3. Converting Array Back to Number: The elements of the `digits` array are joined into a string using `string.Join()`, and then parsed back into an integer using `int.Parse()`. The reversed number is stored in the variable `reversedNumber`.

4. Displaying Reversed Number: Finally, the reversed number is printed using `Console.WriteLine()` along with an appropriate message.

#### Output:

`Reversed number: 97531`

In this blog, we explored four different methods to reverse numbers in C#: using a while loop, a function, recursion, and arrays. Each approach has its own advantages and use cases, providing flexibility in solving programming problems. Experiment with these methods to gain a deeper understanding of C# programming.

Explore a variety of exciting coding language courses and blogs at Newtum. Discover Coding Courses about Python, HTML, and more. Elevate your coding abilities with us today! Don’t just dream of becoming a programmer, dream of becoming a PRO coder!

**Reverse number in C# – FAQs**

**1. How do I reverse a number in C# using a while loop?****Answer-** To reverse a number using a while loop in C#, you can repeatedly extract the last digit of the number, append it to the reversed number, and remove it from the original number until the original number becomes 0.

**2. Can I reverse a number in C# using a function?****Answer-** Yes, you can define a function in C# to reverse a number. The function will take the number as input, perform the reversal process using a while loop or another method, and return the reversed number.

**3. Is recursion a viable method to reverse a number in C#?****Answer-** Yes, recursion can be used to reverse a number in C#. By recursively calling a function, you can repeatedly extract the last digit of the number, append it to the reversed number, and divide the number until it becomes 0.

**4. How can I reverse a number in C# using arrays?****Answer- **You can convert the number to a string, split it into an array of digits, reverse the array, and then join the digits back into a string. Finally, parse the string back to an integer to get the reversed number.

**5. Which method is the most efficient for reversing numbers in C#?****Answer-** The efficiency of each method depends on factors such as the size of the number and the specific implementation. Generally, using a while loop or arrays may offer better performance compared to recursion for larger numbers.

**6. Are there built-in functions in C# to reverse numbers?****Answer-** No, C# does not have built-in functions specifically designed to reverse numbers. However, you can implement your own functions or use existing methods like converting to strings and arrays to achieve the desired result.