Java Program to reverse a number in Java Programming, you have to ask to the user to enter the number.
Start reversing the number, first make a variable rev and place 0 to rev initially, and make one more variable say rem.
Now place the modulus of the number to rem and place rev*10+rem to the variable rev, now divide the number with 10 and continue until the number will become 0.
import java.util.Scanner;
public class Reverse_Number
{
public static void main(String args[])
{
int n, reverse = 0;
System.out.println("Enter the number to reverse");
Scanner in = new Scanner(System.in);
n = in.nextInt();
while( n != 0 )
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
System.out.println("Reverse of entered number is "+reverse);
}
}
OUTPUT ::
Enter the number to reverse
12345
Reverse of entered number is 54321
SOURCE CODE ::
OUTPUT ::
need an explanation for this answer? contact us directly to get an explanation for this answer