How to Reverse an Integer in Java

Sheeraz Gul Feb 02, 2024
  1. Use Recursion to Reverse an Integer in Java
  2. Use the while Loop to Reverse an Integer in Java
  3. Use the for Loop to Reverse an Integer in Java
How to Reverse an Integer in Java

This tutorial will demonstrate how to reverse an integer in Java without using an array.

When reversing an integer without using the array, there are three steps we need to follow.

  • Find the remainder of the integer using the modulo (%) operator.
  • Multiply the variable reverse with 10 and then add it to the remainder.
  • Divide the number by 10.

Use Recursion to Reverse an Integer in Java

Recursion is the simplest method to reverse an integer with modulo and division operators.

Example:

import java.util.Scanner;
public class Reverse_Recursion {
  // Create a method to reverse a number using recursion
  public static void Recursion_reverse(int input_number) {
    if (input_number < 10) {
      // If the number is less then 10 then no need to reverse
      System.out.println(input_number);
      return;
    } else {
      System.out.print(input_number % 10);
      Recursion_reverse(input_number / 10);
    }
  }
  public static void main(String args[]) {
    System.out.print("Enter the Integer you want to Reverse: ");
    Scanner input_num = new Scanner(System.in);
    int input_number = input_num.nextInt();
    System.out.print("The reverse of the given number using recursion is: ");
    Recursion_reverse(input_number);
  }
}

Output:

Enter the Integer you want to Reverse: 1234567890
The reverse of the given number using recursion is: 0987654321

Use the while Loop to Reverse an Integer in Java

To reverse an integer using a while loop, we must follow all three steps mentioned.

Example:

import java.util.Scanner;
public class Reverse_While {
  public static void main(String args[]) {
    System.out.print("Enter the Integer you want to Reverse: ");
    Scanner input_num = new Scanner(System.in);
    int input_number = input_num.nextInt();
    int reverse_number = 0;
    while (input_number != 0) {
      int remainder_number = input_number % 10;
      reverse_number = reverse_number * 10 + remainder_number;
      input_number = input_number / 10;
    }
    System.out.println("The reverse of the given number using while loop is: " + reverse_number);
  }
}

Output:

Enter the Integer you want to Reverse: 123456789
The reverse of the given number using while loop is: 987654321

Use the for Loop to Reverse an Integer in Java

The for loop also works similar to the while loop reverses an integer.

Let’s try to reverse a negative integer this time.

Example:

import java.util.Scanner;
public class Reverse_For {
  public static void main(String args[]) {
    System.out.print("Enter the Integer you want to Reverse: ");
    Scanner input_num = new Scanner(System.in);
    int input_number = input_num.nextInt();
    int reverse_number = 0;
    for (; input_number != 0; input_number = input_number / 10) {
      int remainder_number = input_number % 10;
      reverse_number = reverse_number * 10 + remainder_number;
    }
    System.out.println("The reverse of the given number using while loop is: " + reverse_number);
  }
}
Enter the Integer you want to Reverse: -123456789
The reverse of the given number using while loop is: -987654321
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Integer