Friday 3 June 2016

Java Program to Reverse an Integer Number without using String - Example

How ot revrse a integer number in Java

When I was asked "Can you write a program to reverse a number in Java" long long years back, I did a trick. I converted the number to String by just concatentating with empty String e.g. "" + 123 and then converted that Stirng to StringBuilder can caled the reverse() method.

That solved the problem but Interviewer was not satisfied. He said Ok, but then asked again, can you reverse a number without using anything String or StringBuilder and by building your logic with basic programming elements. I was confused, what do you mean by basic programming elements? He said, I can use loop, if block, operator, and other basic stuff which makes a programming language.

Unfortunately, I couldn't solve the problem that day because I didn't knew the trick that you can get the last digit of a number by doing modulo 10 and remove the last digit by dividing number by 10. Had I knew that, it would have bee quite easy to reverse the number in Java or any programming language e.g. C++.

Today, I am going to share you the same trick, which you can use in programming problmes where you need to reverse a number e.g. checking if a given number is a palindrome or not.


Solution:

Suppose we need to revrese the number 123456 in Java. The reverse of number is 654321, so last digit become first and first become last. To this we'll use modulo and division operator as shown below:

        int reverse = 0;
        while (number > 0) {
            int lastDigit = number % 10;
            reverse = 10 * reverse + lastDigit;
            number = number / 10;
        }
        return reverse;

You can see that we start with reverse = 0 and then get the last digit of original number and add into reverse after multiplying it by 10. Then we get rid of last digit by dividing the number by 10.  This way, our reverse keep increasing and original number keep decreasing, when original number is no more greater than 0, we stop doing this. At this point of tiem the number is reversed.

How to reverse a number in Java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Scanner;

/*
 * Java Program to reverse a number.
 * You can use modulo(%) and division operator(/)
 * to reverse a number. The trick is number/10 will
 * remove the last digit of number and number%10 gives
 * last digit of number. If you know this two tricks
 * you can easily reverse a number in Java, C or C++.
 */
public class ReverseTheNumber {

    public static void main(String[] args) {

        Scanner commandReader = new Scanner(System.in);
        System.out.println("Hello and Welcome into Java "
                + "Program to check if a number is palindrome");
        System.out.println("Enter the number : ");
        int number = commandReader.nextInt();
        int reverse = reverse(number);
        System.out.println("Reverse of the number is : " + reverse);
        commandReader.close();

    }

    /**
     *
     * A Java method to reverse a number
     *
     *
     * @param number
     *
     * @return reverse of the given number
     *
     */
    public static int reverse(int number) {
        int reverse = 0;
        while (number > 0) {
            int lastDigit = number % 10;
            reverse = 10 * reverse + lastDigit;
            number = number / 10;
        }
        return reverse;
    }

}

Output
Hello and Welcome into Java Program to check if a number is palindrome
Enter the number :
122344
Reverse of the number is : 443221
Hello and Welcome into Java Program to check if a number is palindrome
Enter the number :
89890
Reverse of the number is : 9898

So, this is it about how to reverse a number in Java. The technique learned in this program goes a long way on solving more advanced coding problems on programming challenges.

No comments:

Post a Comment