Reverse Number program in C sharp – Learn how to reverse a given input number using for , while, do-while loops, function and recursion.
For example if you have a number 597, then reverse of that number is 795.
Reverse of 597=795.
Reverse Number program examples in C sharp
Reverse Number program using while loop
using System;
class Program
{
static void Main(string[] args)
{
int number = 359;
int revereNumber = 0;
int remainder = 0;
Console.WriteLine("Number before reverse = " + number);
while(number > 0)
{
remainder = number % 10;
number = number / 10;
revereNumber = revereNumber * 10 + remainder;
}
Console.WriteLine("Number after reverse = "+ revereNumber);
}
}
Output
Number before reverse = 359
Number after reverse = 953
Reverse Number program using for loop.
using System;
class Program
{
static void Main(string[] args)
{
//initializing num by 359
int number = 359;
int revereNumber = 0;
int remainder = 0;
Console.WriteLine("Number before reverse = " + number);
//reverse number logic
for (remainder = 0; number > 0;number = number / 10)
{
remainder = number % 10;
revereNumber = revereNumber * 10 + remainder;
}
Console.WriteLine("Number after reverse = " + revereNumber);
}
}
Reverse Number program using do-while loop.
class Program
{
static void Main(string[] args)
{
//initializing num by 359
int number = 359;
int revereNumber = 0;
int remainder = 0;
Console.WriteLine("Number before reverse = " + number);
//reverse number logic
if (number > 0)
do
{
remainder = number % 10;
number = number / 10;
revereNumber = revereNumber * 10 + remainder;
}while(number > 0);
Console.WriteLine("Number after reverse = " + revereNumber);
}
}
Reverse Number program using function.
class Program
{
static void Main(string[] args)
{
//initializing num by 359
int number = 359;
int revereNumber = 0;
Console.WriteLine("Number before reverse = " + number);
Program p = new Program();
revereNumber = p.reverse(number);
Console.WriteLine("Number after reverse = " + revereNumber);
}
//reverse function definition
int reverse(int num)
{
int revereNumber = 0;
int remainder = 0;
while (num > 0)
{
remainder = num % 10;
num = num / 10;
revereNumber = revereNumber * 10 + remainder;
}
return revereNumber;
}
}
Reverse Number program using recursion.
Method -1
using System;
class Program
{
static void Main(string[] args)
{
//initializing num by 359
int number = 359;
int len = number.ToString().Length;
int revereNumber = 0;
Console.WriteLine("Number before reverse = " + number);
Program p = new Program();
revereNumber = p.reverse(number, len);
Console.WriteLine("Number after reverse = " + revereNumber);
}
//reverse function definition
int reverse(int num,int limit)
{
if (limit == 1)
return num;
else
return (num%10)*(int)Math.Pow(10,limit-1)+reverse(num / 10, --limit); //recursive function.
}
}
Method 2
class Program
{
public int sum = 0, remainder;
static void Main(string[] args)
{
int number = 359;
int revereNumber = 0;
Console.WriteLine("Number before reverse = " + number);
Program p = new Program();
revereNumber = p.reverse(number);
Console.WriteLine("Number after reverse = " + revereNumber);
}
//reverse function definition
int reverse(int num)
{
if (num > 0)
{
remainder = num % 10;
sum = sum * 10 + remainder;
reverse(num / 10);
}
else
return sum;
return sum;
}
}