Reverse of number in Java

Source code :

class Reverse
{
   public static void main(String args[])
   {
      int n=103, reverse = 0;
 
       while( n != 0 )
      {
          reverse = reverse * 10;
          reverse = reverse + n%10;
          n = n/10;
      }
 
      System.out.println("Reverse of entered number is "+reverse);
   }
}

Output :

Reverse of entered number is 301

Algorithm :

  1. Enter the required variables
  2. Use the formula of reverse while(n != 0) { remainder = n%10; reversedNumber = reversedNumber*10 + remainder; n /= 10; }
  3. Run the program
  4. Print the result

Output :


                

Comments :