# Program to Reverse a Number and String in C#

In this article, we’ll explore how to reverse a number and string in C# with clear code examples and detailed explanations. Understanding these fundamental concepts is crucial whether you’re an experienced developer or just starting. Let’s dive in.

## Reverse a Number in C# Using Arithmetic Operations

Here is the code sample showing how to reverse a number in C#.

We’ll create a simple C# program that takes an input number from the console and uses a while loop to reverse it using basic arithmetic operations.

``````using System;

namespace Reverse_Number_Program
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a Number: ");
int reminder, reverse = 0;

// Loop until the original number becomes 0
while (number > 0)
{
// Extract the last digit of the number (remainder when divided by 10)
reminder = number % 10;

// Build the reversed number by multiplying the current reversed number by 10
// and adding the extracted digit
reverse = (reverse * 10) + reminder;

// Remove the last digit from the original number
number = number / 10;
}

Console.WriteLine(\$"The Reverse order is: {reverse}");
}
}
}
``````

Output:

``````Enter a Number: 12345
The Reverse order is: 54321``````

## Reverse a Number in C# Using String Manipulation

``````using System;

class Program
{
static void Main()
{
int number = 12345;
string reversedNumberString = ReverseNumber(number);

Console.WriteLine(\$"Original Number: {number}");
Console.WriteLine(\$"Reversed Number: {reversedNumberString}");
}

static string ReverseNumber(int num)
{
char[] charArray = num.ToString().ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
``````

Output:

``````Original Number: 12345
Reversed Number: 54321``````

## Reverse a String in C#

Now, let’s tackle string reversal. We’ll take a string as input from the console and reverse it using a for loop.

Here’s the code to reverse a string in C#:

``````using System;

namespace Program_To_Revrse_String
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a String: ");
string reverse = string.Empty;

for (int i = name.Length - 1; i >= 0; i--)
{
reverse += name[i];
}

Console.WriteLine(\$"The Reverse string is: {reverse}");
}
}
}
``````

Output:

``````Enter a String: Hello World
The Reverse string is: dlroW olleH``````

Code Explanation:

• The program prompts the user to enter a string using `Console.ReadLine()` and stores it in the variable `name`.
• The program uses a for loop to iterate through the characters of the input string in reverse order (from the last character to the first). It appends each character to the reverse string.
• The reversed string is then displayed using `Console.WriteLine()`.

## Using the foreach loop to reverse a string in C#:

An alternative approach is to use a `foreach` loop to reverse a string in C#:

``````using System;

namespace ReverseNumberPrograms
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a String: ");
string reverse = string.Empty;

foreach (char c in name)
{
reverse = c + reverse;
}

Console.WriteLine(\$"The Reverse string is: {reverse}");
}
}
}
``````

Output:

``````Enter a String: Hello World
The Reverse string is: dlroW olleH``````

## Reversing a String Using InBuilt Method in C#

Lastly, let’s use the `Array.Reverse` method to reverse a string. We’ll convert the string to a character array, reverse its elements, and create a new string from the reversed array:

``````using System;

namespace ReverseString
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a String: ");
char[] nameArray = name.ToCharArray();
Array.Reverse(nameArray);
string reverse = new string(nameArray);

Console.WriteLine(\$"The Reverse string is: {reverse}");
}
}
}
``````

Output:

``````Enter a String: Hello World
The Reverse string is: dlroW olleH``````

Recommended Articles:

5 1 vote
Article Rating
Subscribe
Notify of