EXPLANATION:-
Here we see how to reverse a program using while loop.
STEP1: First write the class name and declare the main method.
STEP2: Initialize the variable int,num,remainder,reverse for the program.
STEP3: Use while loop and find the remainder value by 10 using the modulo of the given program.
STEP4: Then multiply the reverse number by 10 and add the modulo value what we get in the remainder.
STEP5: Next divide the number by 10.
STEP6: Keep on repeat the process until the zero value will appear.
STEP7: Finally we get the reverse value (EG:654321-123456).
Program:
public class Reverse
{
public static void main(String[] args)
{
int num = 654321, reverse = 0;
while (num != 0)
{
int remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
}
System.out.println(" The reverse of the given number is : " + reverse);
}
}
Compile:
javac Reverse.java
java Reverse
Output:
The reverse of the given number is : 123456