Here, First we take one number from user and assign it to temporary variable called temp.
Now we execute on while loop until number is zero. Inside loop we get the last digit of the number by finding the reminder of the number by performing the modulus by 10, after that we append the digits with the formula rev=(rev*10)+reminder; first time the value of rev is 0. Now we remove last digit which is appended by performing division by 10. Now when loop completely executed the variable rev gives use reverse number.
import java.util.*; public class reverse { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int no,temp,a,rev=0; System.out.println("***Reverse Number***"); System.out.println("Enter any number ="); no=sc.nextInt(); temp=no; System.out.println("Original Number ="+no); while(no!=0) { a=no%10; rev=(rev*10)+a; no=no/10; } System.out.println("Reverse Number is ="+rev); } }
Here is the OutPut
***Reverse Number*** Enter any number = 105412 Original Number =105412 Reverse Number is =214501