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.
Table of Contents
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 number = int.Parse(Console.ReadLine());
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}");
Console.ReadKey();
}
}
}
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 name = Console.ReadLine();
string reverse = string.Empty;
for (int i = name.Length - 1; i >= 0; i--)
{
reverse += name[i];
}
Console.WriteLine($"The Reverse string is: {reverse}");
Console.ReadKey();
}
}
}
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 variablename
. - 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 name = Console.ReadLine();
string reverse = string.Empty;
foreach (char c in name)
{
reverse = c + reverse;
}
Console.WriteLine($"The Reverse string is: {reverse}");
Console.ReadKey();
}
}
}
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: ");
string name = Console.ReadLine();
char[] nameArray = name.ToCharArray();
Array.Reverse(nameArray);
string reverse = new string(nameArray);
Console.WriteLine($"The Reverse string is: {reverse}");
Console.ReadKey();
}
}
}
Output:
Enter a String: Hello World
The Reverse string is: dlroW olleH
Recommended Articles:
- Fibonacci sequence: Fibonacci series in C# (with examples)
- How to Reverse an Array in C# with Examples
- How to remove duplicate characters from a String in C#
- Palindrome program in C# with examples
- C# Program to Check if a Given Number is Even or Odd
- C# Program to Swap Two Numbers (With Multiple Examples)
- C# program to count the occurrences of each character in a String
- C# program to count vowels and consonants in a string
- How to remove special characters from a string in C#
- Program to copy all elements of an array into another array
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- C# Programs asked in Interviews
- Difference Between Array And ArrayList In C#: Choosing the Right Collection - May 28, 2024
- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024