Table of Contents
Java program to reverse a number. Reversing a number involves reversing its digits. For example, if the input number is 1234, the reversed number should be 4321. This can be achieved by repeatedly extracting the last digit of the number and constructing the reversed number.
Java Program to Reverse a Number
Here’s a Java program that prompts the user to enter a number and then reverses that number.
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a number
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Reverse the number
int reversedNumber = reverseNumber(number);
// Print the result
System.out.println("The reversed number is: " + reversedNumber);
}
// Method to reverse a number
public static int reverseNumber(int n) {
int reversed = 0;
while (n != 0) {
int digit = n % 10; // Extract the last digit
reversed = reversed * 10 + digit; // Append the digit
n /= 10; // Remove the last digit
}
return reversed;
}
}
Explanation of the Code: (Java program to reverse a number)
Let’s break down the program step-by-step to understand how it works.
- Importing the Scanner Class: We import the
Scanner
class from thejava.util
package to read input from the user.
import java.util.Scanner;
2. Creating the Main Class: We create a public class named ReverseNumber
. In Java, the class name should match the filename.
public class ReverseNumber {
3. Main Method: The main
method is the entry point of the program. Inside this method, we will write the code to reverse a number.
public static void main(String[] args) {
4. Creating a Scanner Object: We create a Scanner
object to read input from the user.
Scanner scanner = new Scanner(System.in);
5. Reading Input from the User: We prompt the user to enter a number and store this value in a variable.
System.out.print("Enter a number: ");
int number = scanner.nextInt();
6. Reversing the Number: We call the reverseNumber
method to reverse the digits of the number. The result is stored in a variable reversedNumber
.
int reversedNumber = reverseNumber(number);
7. Printing the Result: We print the reversed number.
System.out.println("The reversed number is: " + reversedNumber);
8. Method to Reverse a Number: The reverseNumber
method reverses the digits of the number. This method works by repeatedly extracting the last digit of the number and constructing the reversed number.
public static int reverseNumber(int n) {
int reversed = 0;
while (n != 0) {
int digit = n % 10; // Extract the last digit
reversed = reversed * 10 + digit; // Append the digit
n /= 10; // Remove the last digit
}
return reversed;
}
Explanation of the reverseNumber
Method
- Initializing the Reversed Number: We initialize a variable
reversed
to 0. This variable will store the reversed number.
int reversed = 0;
2. Loop to Reverse the Number: The while
loop continues as long as n
is not zero. In each iteration, we extract the last digit of n
using n % 10
, append it to reversed
, and then remove the last digit from n
by dividing it by 10.
while (n != 0) {
int digit = n % 10; // Extract the last digit
reversed = reversed * 10 + digit; // Append the digit
n /= 10; // Remove the last digit
}
3. Returning the Reversed Number: After the loop finishes, reversed
contain the reversed number. We return reversed
.
return reversed;
Running the Program (Java program to reverse a number)
To run the program, follow these steps:
- Save the code in a file named
ReverseNumber.java
. - Open a command prompt or terminal and navigate to the directory where you saved the file.
- Compile the program using the following command:
javac ReverseNumber.java
4. Run the compiled program using the following command:
java ReverseNumber
5. Follow the prompt to enter a number. The program will display the reversed number.
Example Output
If you run the program and enter 1234 as the number, the output will be:
Enter a number: 1234
The reversed number is: 4321
If you run the program and enter 98765 as the number, the output will be:
Enter a number: 98765
The reversed number is: 56789
What did we learn from this article?
Reversing a number is a fundamental exercise that helps in understanding loops and arithmetic operations in programming. This Java program demonstrates how to reverse the digits of a number by extracting each digit and constructing the reversed number. By practicing such basic programs, you can strengthen your problem-solving skills and gain confidence in your ability to write efficient Java code. Keep practicing and exploring more problems to enhance your programming skills.
Top 100 Java Programs: Click Here
Are you looking for a job: Click Here