## Table of Contents

Here, we’ll go over the Java code for reversing a number using the while loop, the for loop, and recursion.

Here are the measures to reverse a number:

To begin, we use the modulo (%) operator to determine the remaining fraction of the supplied number.

The variable reverse should be multiplied by 10 before the remainder is added.

Estimate the answer by dividing it by 10.

Iterate the preceding steps till the result is 0.

In Java, you can reverse a number in three different ways:

Reverse a number using while loop

Reverse a number using for loop

Reverse a number using recursion

Enroll in the Java course in Chennai at SLA and learn more about Java programming language to level up your career under committed faculties, extensive training, nd dedicated placement support.

To put the above into context,

Example

Let’s say we need to reverse the number 1234 around.

In this illustration, we’ve used three variables with initial values of 0: number (the number to be reversed), remainder (which holds the remainder), and reverse (which stores the reverse number).

number = 1234

remainder = 1234 % 10 = 4

reverse = 0 * 10 + 4 = 0 + 4 = 4

number = 1234 / 10 = 123

The number and reverse variables now have values of 123 and 4, respectively.

number = 123

remainder = 123 % 10 = 3

reverse = 4 * 10 + 3 = 40 + 3 = 43

number = 123 / 10 = 12

The number and reverse variables now have values of 12 and 43, respectively.

number = 12

remainder = 12 % 10 = 2

reverse = 43 * 10 + 2 = 430 + 2 = 432

number = 12 / 10 = 1

Currently, the value of the number variable is 1 and the value of the reverse variable is 432.

number = 1

remainder = 1 % 10 = 1

reverse = 432 * 10 + 1 = 4320 + 1 = 4321

number = 1 / 10 = 0

It seems like the number has settled to zero. This gives us the reverse of the number, which is 4321.

It would be easy to put the following reasoning into a Java program. Join Java training in Chennai and acquire practical coaching knowledge.

## 1Reverse a number using while loop

public class ReverseNumberExample1

{

public static void main(String[] args)

{

int number = 341989, reverse = 0;

while(number != 0)

{

int remainder = number % 10;

reverse = reverse * 10 + remainder;

number = number/10;

}

System.out.println(“The reversed number is: ” + reverse);

}

}

Output

The Reverse Number is 989143

Enroll in the Java course in Chennai to learn everything about Java with practical training.

## 2Reverse a number using for loop

We’ve switched to using a for loop instead of a while loop in the code below. Each repetition also eliminates the last digit of the integer. As soon as the comparison with zero fails (number!=0), the loop terminates and the reverse of the input is returned.

public class

{

public static void main(String[] args)

{

int number = 13579, reverse = 0;

//we have not mentioned the initialization part of the for loop

for( ;number != 0; number=number/10)

{

int remainder = number % 10;

reverse = reverse * 10 + remainder;

}

System.out.println(“The reversed number is: ” + reverse);

}

}

Output

The Reverse Number is 97531

The above program can also be written by using the for loop as follows:

for(;number != 0;)

{

int remainder = number % 10;

reverse = reverse * 10 + remainder;

number=number/10;

}

Join the best Java training in Chennai to land on your dream job with SLA’s placement assistance.

## 3Reverse a number using recursion

import java.util.Scanner;

public class ReverseNumberExample3

{

//method for reverse a number

public static void reverseNumber(int number)

{

if (number < 10)

{

//prints the same number if the number is less than 10

System.out.println(number);

return;

}

else

{

System.out.print(number % 10);

reverseNumber(number/10);

}

}

public static void main(String args[])

{

System.out.print(“Enter the number that need to be reversed: “);

Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

System.out.print(“Thegiven number is reversed as: “);

//method calling

reverseNumber(num);

}

}

Output 1:

Enter the number that you want to reverse: 9

The reverse of the given number is: 9

Output 2:

Enter the number that you want to reverse: 7654123

The Reverse Number is 3214567

The following code reverses positive and negative values. It first determines whether the number we enter is positive or negative when we enter it. It multiplies the negative value by -1 to make it positive if necessary. After that, it executes the identical procedures (that we have performed in the prior programs) to reverse a number. Finally, it double-checks if the input is positive or negative. The number is made negative by multiplying the reverse by -1 once more.

import java.util.*;

public class ReverseNumberExample4

{

public static void main(String args[])

{

System.out.print(“Enter the number that need to be reversed: “);

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int reverse_number = reverseNumber(n);

System.out.println(“The reverse of the number is: “+reverse_number);

}

//method to reverse a number

public static int reverseNumber(int number)

{

boolean isNoNegative = number < 0 ? true : false;

if(isNoNegative)

{

number = number * -1; //turns the negative number into a positive number if the given variable is negative

}

int reverse = 0;

int lastDigit = 0;

while (number >= 1)

{

lastDigit = number % 10; // gives the last digit of the number

reverse = reverse * 10 + lastDigit;

number = number / 10; // removes the last digit of the number

}

//makes the number negative

return isNoNegative == true? reverse*-1 : reverse;

}

}

Output 1:

Enter the number that you want to reverse: -246801

The reverse of the given number is: -108642

Output 2:

Enter the number that you want to reverse: 98765

The Reverse of the given number is: 56789

Enroll in the best Java training institute in Chennai and hone your java coding skills.