Introduction
The first example shows how to reverse a number in C#. The second example shows how to reverse a string in C#.
Reverse a Number in C#
Here is the code sample that shows you to reverse a number in C#.
using System;
namespace ReverseNo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number to reverse");
int Number = int.Parse(Console.ReadLine());
int Reverse = 0;
while (Number > 0)
{
int remainder = Number % 10;
Reverse = (Reverse * 10) + remainder;
Number = Number / 10;
}
Console.WriteLine("Reversed number is {0}", Reverse);
Console.ReadLine();
}
}
}
Output

Reverse a String in C#
The following code sample shows how to reverse a string in C#.
using System;
namespace reverseString
{
class Program
{
static void Main(string[] args)
{
string str = "", reverse = "";
int Length = 0;
Console.WriteLine("Enter a Word");
// Getting String (word) from Console
str = Console.ReadLine();
// Calculate length of string str
Length = str.Length - 1;
while (Length >= 0)
{
reverse = reverse + str[Length];
Length--;
}
// Displaying the reverse word
Console.WriteLine("Reverse word is {0}", reverse);
Console.ReadLine();
}
}
}
Output

Hope you like it. Thanks.

Dilip PatelPosted Oct 10, 2023, 4:49 AM
Sorry Anoop , Reverse a Number will not work for 12340
Sadiqullah GhaniPosted Jan 21, 2019, 1:28 AM
Why we divide by 10 in number reverse order
vijaya patilPosted Oct 14, 2018, 12:04 AM
As im fresher im not getting that while loop condition.. Can You Plz explain??
Jogesh SahuPosted Jun 7, 2018, 1:16 AM
This is not working for Number like mutiple of 10 Eg.4000 means it returning 4 instead of 0004,
D ShashankPosted May 18, 2018, 3:12 PM
Hi This dosent work if i give multiples of 10 it hides the zero for Eg i give 40 it shows the reverse number is 4
Lingaswamy MPosted Sep 21, 2015, 1:13 AM
Thanq, I am beginner to C#, this is very helpful to me.
Damodara NaiduPosted Nov 4, 2014, 4:51 AM
using linq ... str = new string(str.ToCharArray().Reverse().ToArray()); Console.WriteLine("Reverse word is {0}",str);
Damodara NaiduPosted Nov 4, 2014, 2:47 AM
try this .... string str = Console.Read(); var chrArray = str.ToCharArray(); Array.Reverse(chrArray); str= new String(chrArray); Console.WriteLine("Reverse word is {0}",str); Console.ReadLine();
Michal HabalcikPosted Nov 4, 2014, 1:59 AM
good examples. However, why do you have such inconsistency in naming the variables? That can be confusing for some beginners. Some of the variables start with capital letter and some don't.