Write a program to Reverse a Number using array in Java

Here we are using array to reverse a number in Java.

Code:
 public class ReverseNumberA {  
      public static void main(String args[])  
      {   
           reverseNum(1203450);  
      }  
      public static void reverseNum(int num)  
      {  
           int length=String.valueOf(num).length();  
           int number[]=new int[length];  
           for(int n=0;n<=number.length-1;n++)  
           {  
                number[n]=num%10;  
                num=num/10;  
           }  
           System.out.println("Number after reverse :");  
           for(int n:number)  
           {  
                System.out.print(n);  
           }  
      }  
 }  
Output:
Number after reverse :
0543021

No comments: