Day 8 - Java

Problem: Number Reversal

You are given an integer number. Your task is to reverse the digits of this number and return the result. However, there's a twist! You need to implement the reversal algorithm without using any loops, recursion, or built-in reverse functions.

Write a Java program that takes an integer input and outputs its reverse using only lists and strings. Additionally, you are not allowed to use any library functions to directly reverse the number.

Input Format:

A single integer, 

Output Format:

Print the reversed number.

Sample Input:

9876543210

Sample Output:

123456789

Explanation:

The reverse of the number 9876543210 is 123456789.


SOLUTION:

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;


public class NumberReversal {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int num = sc.nextInt();


        int result = reverseNumber(num);

        System.out.println(result);

    }


    public static int reverseNumber(int n) {

        String ns = String.valueOf(n);


        List<Character> cl = new ArrayList<>();

        for (char c : ns.toCharArray()) {

            cl.add(c);

        }


        List<Character> rl = new ArrayList<>();

        for (int i = cl.size() - 1; i >= 0; i--) {

            rl.add(cl.get(i));

        }


        StringBuilder rs = new StringBuilder();

        for (char c : rl) {

            rs.append(c);

        }


        int rn = Integer.parseInt(rs.toString());

        return rn;

    }

}


Insights:

  • The integer input is converted to a String using the valueOf method of the String class.
  • The characters of the input number are stored in a list of characters (List<Character>), named cl, by iterating over the characters of the input String.
  • Another list, rl, is used to store the characters of the input number in reverse order. This is achieved by iterating over the cl list in reverse and adding each character to the rl list.
  • A StringBuilder (rs) is used to construct the reversed string by appending each character from the rl list.
  • Finally, the reversed string is converted back to an integer using the parseInt method of the Integer class.
  • ArrayLists are used to store the characters of the input number and its reversed form. ArrayList provides dynamic resizing, making it suitable for this task.
  • The code leverages the primitive type char and its wrapper class Character to handle individual characters efficiently.

Explore:

In the above program explore why 001 is reversed as 1 and not 100 and also 100 is also reversed as 1 and not 001! THINK!\

The while loop coding to reverse the number

import java.util.Scanner;

public class NumberReversal {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int num = sc.nextInt();


        int result = reverseNumber(num);

        System.out.println(result);

    }


    public static int reverseNumber(int n) {

        int reversed = 0;

        while (n != 0) {

            int digit = n % 10;

            reversed = reversed * 10 + digit;

            n /= 10;

        }

        return reversed;

    }

}


Enjoy and Explore Coding!! :)

Comments