Dev Dev - 6 months ago 13
Java Question

To print the reverse of a given number in java lang

I have found examples which imported java.util.scanner and used reverse and scanner keys. Im a beginner and I tried a simpler way, though I am not getting the right output. please find the mistake in my code.

public class ReverseNum {

public static void main(String[] args) {
int i=234,n,x;

n=i%10;
x=n;

while(i>=0){

i=(i/10)-n;
n=i%10;
x=(x*10)+n;
System.out.print(x);

}

}


}

Answer
public static void main(String[] args) {
        int i = 234, n;
        while (i > 0) {
            n = i % 10;
            i = i / 10;
            System.out.print(n);
        }
}

hope this is what you wanted...