The program first imports the java.util.Scanner package to allow user input. It then declares a class called Digits_Reverse and a main method, which is the entry point of the program.
Within the main method, the program prompts the user to enter a number and stores the input in the variable num. It then initializes a variable a to num to store the original value of num, a variable rev to 0 to store the reversed value, and a variable rem to 0 to store the remainder of num when divided by 10.
Next, the program runs a while loop that continues as long as num is greater than 0. Within the loop, the program calculates the remainder of num when divided by 10 using the modulus operator, and adds it to rev multiplied by 10 to add the current digit to the reversed number. It then divides num by 10 to remove the last digit from the number and continue the loop. Finally, the program prints out both the original number and the reversed number.
import java.util.Scanner; class Digits_Reverse { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the Digits :"); int num = input.nextInt(); int a = num; int rev = 0; int rem = 0; while(num>0) { rem = num % 10; rev = rev * 10 + rem; num /= 10; } System.out.println("Given Digits :" + a); System.out.println("Reverse Digits :" + rev); } }
Enter the Digits :345785 Given Digits :345785 Reverse Digits :587543
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions