In this tutorial you will learn about the Java Program to Reverse Given Number and its application with practical example.
Java Programs to Reverse Given Number
In this tutorial, we will learn to create a Java program that will reverse Given Number using Java programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
- While loop in Java programming.
- Conditional Statements in Java programming.
- Arithmetic operations in Java Programming.
Program to reverse any number:-
In this program, we will, first, declare the variables for the program. Then we will assign the values to the variables. Now we will use the while loop to reverse the number using the arithmetic expression. At last, we will print the output to the user.
With the help of this program, we can reverse any Number.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the required variables for the program. 2. Taking the input number from the user. 3. Passing that number to a while loop. 4. Reversing that input number. 5. Printing the reversed number to the user. 6. End the program. |
Program to reverse any number:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//Java Program to Reverse Given Number class RevNo {//Class for the program. //Body of the main function for the program. public static void main(String[] args) { // Declaring the required variables for the program. int no,rv; // Assigning the values to the variables. no = 1234; rv = 0; // no,rv = it will hold the integer value for the program. // printing the original value to the user. System.out.println("Original Number: " + no); // run loop until no becomes 0. // Reversing the number using the while loop. while(no != 0) { // get last digit from no. int digit = no % 10; rv = rv * 10 + digit; // remove the last digit from no. no /= 10; } // Printing the reversed number to the user. System.out.println("Reversed Number: " + rv); } } |
Output:-
In the above program, we have first initialized the required variable.
- n = it will hold the integer value.
- rv = it will hold the integer value.
Input message for the user for the integer value.
Program Logic Code.
Printing output reverses any Number.