Skip to content

Reverse a Number

Reverse the digits of an integer using a while loop.

package DSA.Flow_control;
import java.util.Scanner;
public class ReverseofNum {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the number");
int n = sc.nextInt();
int rev = 0;
while (n > 0) {
int rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
System.out.println(rev);
} catch (Exception e) {
System.out.println(e);
}
}
}