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.
Using a for loopYou can set a variable in the for loop to hold the reversed number, and then use a loop to separate out each digit from the original number and add it to the reversed number. Until all of the digits have been processed, the loop is repeated.
Using the While LoopThe while loop functions in a manner similar to the for loop. The inverted number is initialised in a variable, and each digit is extracted and added to the reversed number using a loop. Until all of the digits have been processed, the loop is repeated.
Do-While Loop UseThe do-while loop functions in a manner similar to that of the while loop. The condition is not assessed until after at least one execution of the loop body.
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
Explanation:In this program, the input number is reversed by first extracting the last digit of the number using the modulus operator (%) and then adding it to the reversed number. The reversed number is then multiplied by 10 and the process is repeated for each remaining digit. The final result is the reversed number.
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
Explanation:In the while loop, we perform the following steps until num is 0:
We get the last digit of num using the modulo operator %.
We add this digit to reversed after multiplying it by 10. This appends the digit to the right side of reversed.
We divide num by 10 to remove the last digit and move to the next digit.
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
Explanation:The Main class has a main method that takes an array of String arguments args as input.
Inside the main method, an integer number is initialized with a value of 78558.
The reverseNumber method is called with number as an argument and the result is stored in an int variable reverse.
The reversed number is printed to the console using System.out.println with a message "Reverse of number is: reverse".
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment