Program to reverse a number in java with explanation

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:

  1. 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.
  2. Java 8 Approach with IntStream:
    • Here we are taking the power of Java 8 features. We have used IntStream to iterate through the digits of the input number, mapped them to individual digits, and then used the reduce operation to generate the reversed number.
  3. 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! 🚀

Leave a Comment