Java Program to Reverse a Number
Program to Reverse a Number In Java.
In Java, there are three types of loops: for loop, while loop, and do-while loop.
- For Loop: The for loop is used to repeat a block of code for a specified number of times.
- While Loop: The while loop is used to repeat a block of code while a certain condition is true.
- Do-While Loop: The do-while loop is similar to the while loop, but it is guaranteed to execute at least once.
In Java, loops are used to execute a block of code repeatedly based on a given condition. To know more about Java program to Reverse a Number read the complete article.
How Loop, while loop and do-while loop is used to reverse a Number In java
Java allows the use of various looping structures, including the for, while, and do-while loops, to reverse a number.
How to Reverse a Number using Java:
The Java program to reverse a number works by using a while loop and arithmetic operations to reverse the digits of an integer input.
- The program starts by reading in the number using a Scanner object and storing it in an int variable number.
- The while loop then operates on number until it becomes 0. The loop body contains the logic for reversing the number.
- In each iteration of the loop, the last digit of the number is extracted using the modulus operator (%) and stored in a new int variable digit. This operation finds the remainder when number is divided by 10, which is always the last digit of number.
- The reverse variable is then updated by multiplying its current value by 10 and adding the digit to it. This effectively appends the extracted digit to the end of reverse.
- The number is then divided by 10 using integer division (/) to remove the last digit and prepare it for the next iteration of the loop.
- The loop continues until number becomes 0, at which point all of its digits have been extracted and reversed.
- Finally, the reversed number is printed to the console using a println statement.
Note that this program assumes the input number is positive, and will not work correctly for negative numbers. To handle negative numbers, you would need to add additional logic to preserve the sign of the original number.
Let’s look at a Java program to Reverse a Number
Example: Java Program to Reverse a Number
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter a number: "); int number = scan.nextInt(); int reverse = 0; while (number != 0) { int digit = number % 10; reverse = reverse * 10 + digit; number /= 10; } System.out.println("The reversed number is: " + reverse); scan.close(); } }
Output
Enter a number: 5356 The reversed number is: 6535
Example 2 : Program to Reverse a Number using a while loop in Java
public class Main { public static void main(String[] args) { int num = 56789; int reverse = 0; while (num != 0) { int digit = num % 10; reverse = reverse * 10 + digit; num /= 10; } System.out.println("Reversed Number: " + reverse); } }
Output
Reversed Number: 98765
Example 3: how to use the object clone property:
public class Main { public static void main(String[] args) { int number = 78558; int reverse = reverseNumber(number); System.out.println("Reverse of " + number + " is: " + reverse); } public static int reverseNumber(int number){ int reverse = 0; for(;number != 0; number /= 10){ reverse = reverse * 10 + number % 10; } return reverse; } }
Output
Reverse of 78558 is: 85587
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment