lol...
Since this is Java, your int temp and int n both have reference to the same memory pointer.
And when you exit the following loop - both temp and n values are at 0 (or less)
When you first assign temp value, try this instead.
int temp = Integer.valueOf(n);
Yes Java (and C#) are retarded like that... they are built to get rid of manual memory management and pointers, but then introduce shit like this for God knows what reason....
good luck and have fun
EDIT:
umm... didn't read the OP careful enough, guess your problem is solved down below, but it's still okay to know about this stuff.
Your problem is this loop
while(temp>0){ //while loop to count number of digits and form multiple of 10 to do modulus division
temp=temp%10;
d=d*10;
} //loop ends
The modulo operator, %, returns the remainder when divided, so 5 % 10 = 5. This means that you have an infinite loop.
To fix it, you need to use / rather than %, so it will divide by 10 each time (and it uses integer division since the arguments are integers, so it rounds down).