Tutorials

Java Basic Programs


Reverse Number


In this tutorial, our task is:

  • to reverse the given number
  • print the reversed number

Example:

Give number: 1345

Reverse number: 5431

 

Given below is the java program to reverse the given number.



public class ReverseNumber {

 

      public static void main(String[] args) {

            int n = 1345;

            int reverse=0;

 

            while(n!=0){

                  reverse = reverse*10 + n%10;

                  n = n/10;

            }

 

            System.out.println(reverse);

      }

 

}

 

 

 



Please Share this page
Views : 45