Introduction
Reversing a number involves rearranging its digits in the opposite order. This is a common task in programming, often used in algorithms, data processing, and more. This guide will show you how to create a Java program that reverses a given number using different approaches.
Problem Statement
Create a Java program that:
- Takes an integer input from the user.
- Reverses the digits of the number.
- Displays the reversed number.
Example 1:
- Input:
12345
- Output:
54321
Example 2:
- Input:
9876
- Output:
6789
Approach 1: Using a while
Loop
Java Program
import java.util.Scanner;
/**
* Java Program to Reverse a Number using while loop
* Author: https://www.javaguides.net/
*/
public class ReverseNumberWhileLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Prompt the user for input
System.out.print("Enter a number to reverse: ");
int number = scanner.nextInt();
// Step 2: Reverse the number using a while loop
int reversedNumber = 0;
while (number != 0) {
int digit = number % 10; // Extract the last digit
reversedNumber = reversedNumber * 10 + digit; // Build the reversed number
number /= 10; // Remove the last digit from the original number
}
// Step 3: Display the reversed number
System.out.println("Reversed number: " + reversedNumber);
}
}
Explanation
- Input: The program prompts the user to enter a number.
- Reversing: The
while
loop extracts the last digit of the number using the modulus operator (%
). It then constructs the reversed number by appending each digit to the new number. The original number is divided by 10 to remove the last digit. - Output: The reversed number is then printed to the user.
Output Example
Enter a number to reverse: 12345
Reversed number: 54321
Approach 2: Using StringBuilder
's reverse()
Method
Java Program
import java.util.Scanner;
/**
* Java Program to Reverse a Number using StringBuilder
* Author: https://www.javaguides.net/
*/
public class ReverseNumberStringBuilder {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Prompt the user for input
System.out.print("Enter a number to reverse: ");
int number = scanner.nextInt();
// Step 2: Reverse the number using StringBuilder's reverse method
String reversed = new StringBuilder(String.valueOf(number)).reverse().toString();
// Step 3: Convert the reversed string back to an integer and display it
int reversedNumber = Integer.parseInt(reversed);
System.out.println("Reversed number: " + reversedNumber);
}
}
Explanation
- Input: The program prompts the user to enter a number.
- Reversing: The number is first converted to a string using
String.valueOf()
, then reversed usingStringBuilder
'sreverse()
method. - Conversion: The reversed string is converted back to an integer using
Integer.parseInt()
. - Output: The reversed number is then printed to the user.
Output Example
Enter a number to reverse: 9876
Reversed number: 6789
Approach 3: Using Recursion
Java Program
import java.util.Scanner;
/**
* Java Program to Reverse a Number using Recursion
* Author: https://www.javaguides.net/
*/
public class ReverseNumberRecursion {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Prompt the user for input
System.out.print("Enter a number to reverse: ");
int number = scanner.nextInt();
// Step 2: Reverse the number using recursion
int reversedNumber = reverseNumber(number, 0);
// Step 3: Display the reversed number
System.out.println("Reversed number: " + reversedNumber);
}
// Recursive method to reverse the number
public static int reverseNumber(int number, int reversed) {
if (number == 0) {
return reversed;
}
int digit = number % 10;
reversed = reversed * 10 + digit;
return reverseNumber(number / 10, reversed);
}
}
Explanation
- Input: The program prompts the user to enter a number.
- Reversing: The
reverseNumber
method is a recursive function that takes the current number and the reversed number (initially 0) as arguments. The base case is when the number is reduced to 0, at which point the reversed number is returned. - Recursive Case: The method extracts the last digit of the number, appends it to the reversed number, and recursively calls itself with the remaining digits.
- Output: The reversed number is then printed to the user.
Output Example
Enter a number to reverse: 12345
Reversed number: 54321
Conclusion
This Java program provides multiple methods to reverse a number, demonstrating different techniques such as loops, StringBuilder
, and recursion. Each approach has its own advantages, and the choice of method may depend on specific requirements, such as readability, performance, or coding style. These methods are useful for various number manipulation tasks in Java.
Comments
Post a Comment
Leave Comment