Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me how I can able to reverse a number in Java? I am using an array for that, but it is not able to return the values.

1 Answer

0 votes
by (26.7k points)

You can use the below code to reverse a number using an array:

public static void reverse(int[] array) {

    if (array == null) {

        return;

    }

    int i = 0;

    int j = array.length - 1;

    int temp;

    while (j > i) {

        temp = array[j];

        array[j] = array[i];

        array[i] = temp;

        j--;

        i++;

    }

}

I hope this will help.

Want to know more about Java? Prefer this tutorial on Learn Java.

Want to become a Java Expert? join Java Certification now!!

Related questions

0 votes
0 answers
asked Feb 18, 2021 in Python by Harsh (1.5k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 10, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer

Browse Categories

...