January 22, 2015

Interview Question : How to Reverse a Integer Using Basic Java with Reusable Code


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.


/**
*
* @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("");
}
}
view raw ReverseInt.java hosted with ❤ by GitHub

Related Posts:

0 comments:

Post a Comment

Leave your comment and feedback here for me