In this tutorial we are going to learn how to write a program to reverse the digits of a given number in Java programming language.
So Lets know what we are going to achieve in this program before moving on writing program.
Suppose if someone given input to the program like 456789. After performing reverse operation program our Java program will return output 987654.
We are following logic here like first we will take last digit of the input number and store it in a variable. Now we will divide it by 10 so that we can find middle number if exist as last number and append it in previous stored number.
By following this approach we can find reverse of number.
Program 1: Java program to reverse a number
import java.util.*;
class Main {
public static void main(String ...a){
int reverse=0;
Scanner sc = new Scanner(System.in);
System.out.println("Please give a number: ");
int i = sc.nextInt();
while(i!=0){
reverse = reverse*10 + i%10;
i=i/10;
}
System.out.println("Number after reverse : "+reverse);
}
}
Output:
Please give a number:
345214
Number after reverse : 412543
Program 2: Reverse a number using Java 8 (IntStream)
In this program we are using the IntStream.
import java.util.stream.IntStream;
public class ReverseNumber {
public static void main(String ...a) {
int inputNumber = 12345;
IntStream digitStream = IntStream
.iterate(inputNumber, n -> n > 0, n -> n / 10)
.map(n -> n % 10);
int reversedNumber = digitStream
.reduce(0, (acc, digit) -> acc * 10 + digit);
System.out.println(reversedNumber);
}
}
Output:
Output= 54321
Program 3: Using Java 8 (After Converting into String)
In this program we will first convert the Given interger number into String. And the we will be applying String reversal logic using Java 8 to reverse the String.
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ReverseNumber {
public static void main(String ...a) {
int inputNumber = 12345;
String stringIntvalue = String.valueOf(12345);
String reversedString = IntStream.range(0, stringIntvalue.length())
.mapToObj(i -> String.valueOf(stringIntvalue.charAt(stringIntvalue.length() - 1 - i)))
.collect(Collectors.joining(""));
System.out.println("Output= "+reversedString);
}
}
Output
Output= 54321
Conclusion:
In this tutorial, we have learnt various process of reversing a number in Java and Java 8. The goal was to create a program that takes a numeric input and returns its reverse. We explored three different approaches to achieve this:
- Traditional Approach:
- With the help of a while loop, we extracted the last digit of the input number. And then multiplied 10 continuously on last digit number each time and added it to build the reversed number. Also iteratively reduced the input number by digit 1. This method provided a fundamental understanding of the process.
- Java 8 Approach with IntStream:
- Here we are taking the power of Java 8 features. We have used I
ntStream
to iterate through the digits of the input number, mapped them to individual digits, and then used thereduce
operation to generate the reversed number.
- Here we are taking the power of Java 8 features. We have used I
- Java 8 Approach with String Conversion:
- Here we have first converted the Integer into String. And we have utilized the Java 8 stream API to reverse the string character by character.
In this programs we have covered both java old version and java 8 concepts. Whether you prefer a classic iterative method or Java 8 streams, this tutorial will be helpful for you.
Feel free to experiment with these programs, modify them as needed, and integrate the concepts into your Java coding logics. Happy coding! 🚀
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…