# C# Reverse Number

I have given here C# code to reverse a given number. It counts the digits and then it uses / and % operators and simple multiplication to reverse the number.

## Source Code

using System;

using System.Collections.Generic;

using System.Text;

namespace ReverseNumber

{

class ReverseNumber

{

static int CountNumberOfDigits_UsingLog(int number)

{

return (int)(Math.Log10((double)number) + 1);

}

static int CountNumberOfDigits(int number)

{

int numdgits = 0;

do

{

number = number / 10;

numdgits++;

} while (number > 0);

return numdgits;

}

static void Main(string[] args)

{

System.Console.WriteLine("\nProgram to find the reverse of a number. Enter -1 to exit");

while (true)

{

System.Console.Write("Enter a Number (-1 to exit): ");

int n = 0;

try

{

n = Convert.ToInt32(input);

}

catch (System.Exception ex)

{

System.Console.WriteLine("Error in the input format\n\n");

continue;

}

if (n < 0)

break;

int number = n;

int numdigits = CountNumberOfDigits(number);

System.Console.WriteLine("\nNumber of digits in " + number.ToString() + "is : " + numdigits.ToString());

int result = 0;

for (int i = 0; i < numdigits; i++)

{

result *= 10;

result += n % 10;

n = n / 10;

}

System.Console.WriteLine("\nThe reverse of number " + number.ToString() + "is : " + result.ToString());

}

}

}

}