This is simple explanation about how to reverse a order of a integer without using any library method.Mostly this question is asking as a interview question.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @author Gayan | |
*/ | |
public class ReverseInt { | |
private static int count=1; | |
private static boolean found=false; | |
public static void getnumberofdegits(int number){ | |
int i=1; | |
while(!found){ | |
if(number<i*10){ | |
found=true; | |
break; | |
} | |
else{ | |
i=i*10; | |
count++; | |
} | |
} | |
} | |
public static void getreverse(int number){ | |
getnumberofdegits(number); | |
int modulevalue=10; | |
int devider=1; | |
int[] array = new int [count]; | |
for(int i=0;i<count;i++){ | |
array[i]=(number%modulevalue)/devider; | |
modulevalue=modulevalue*10; | |
devider=devider*10; | |
} | |
int printcount=0; | |
while(printcount<count){ | |
System.out.print(array[printcount]); | |
printcount++; | |
} | |
} | |
public static void main(String [] args){ | |
int x=12345678; | |
ReverseInt.getreverse(x); | |
System.out.println(""); | |
} | |
} |
0 comments:
Post a Comment
Leave your comment and feedback here for me