Skip to content

Reverse A Given Number Using While Loop

In order to execute the code I’ve created a simple console application to print out the result.

Please find complete code below:

Input number: 10012

using System;

namespace PracticeConsole
{
   class Program
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Please input any number and then hit enter:");
         int myNumber = Convert.ToInt32(Console.ReadLine());
         int temp, remainder, reverse = 0;
         temp = myNumber;
         while (myNumber > 0)
         {
            remainder = myNumber % 10;
            reverse = reverse * 10 + remainder;
            myNumber /= 10;
         }         
         Console.WriteLine("Reversed number: " + reverse);
         Console.ReadLine();
      }
   }
}

Result: