How To Reverse a Number In Java? Algorithm & Program

3518
Reverse a number in java
Reverse a number in java

How To Reverse a Number In Java? Algorithm & Program

In this blog, we are going to see the Algorithm and different ways to Reverse a number in java. Basically reversing the number is to inverting the number program. The Program for reversing the number can be used to check if a number is a palindrome number or not. If on reversing a number, we get the same number, then it’s a palindrome number; otherwise, not.

Algorithm :

  1. Input:  num
  2. Initialize rev_num = 0
  3. Loop while num > 0
    1. Multiply rev_num by 10 and add remainder of num 
  4. divide by 10 to rev_num
    1. rev_num = rev_num*10 + num%10;
    1. Divide num by 10
  5. Return rev_num

Ways to Reverse a number in java .

  • Using a while loop.
  • Using for a loop.
  • Using recursion.

Reverse a number in java using a while loop (Without user interaction)

This is the program when the number already initialized. In this Program, you just need to change the value of the variable num as per your requirement.

public class ReverseNumber {
    public static void main(String[] args) {
        int num = 1234, reversed = 0;
        while(num != 0) {
            int digit = num % 10;
            reversed = reversed * 10 + digit;
            num /= 10;
        }
        System.out.println("Reversed Number: " + reversed);
    }
}

Reverse a number in java using a while loop (User interaction)

The program will prompt a user to input the number and then it will reverse the same number using a while loop.

Java Feature Spotlight: Text Blocks
import java.util.Scanner;
class ReverseNumberWhile
{
   public static void main(String args[])
   {
      int num=0;
      int reversenum =0;
      System.out.println("Input your number and press enter: ");
      Scanner in = new Scanner(System.in);
      num = in.nextInt();
      while( num != 0 )
      {
          reversenum = reversenum * 10;
          reversenum = reversenum + num%10;
          num = num/10;
      }

      System.out.println("Reverse of input number is: "+reversenum);
   }
}

Reverse a number in java using a for loop (Without user interaction)

This is the program when the number already initialized. In this Program, you just need to change the value of the variable num as per your requirement.

public class ReverseNumber {
    public static void main(String[] args) {
        int num = 1234567, reversed = 0;
        for(;num != 0; num /= 10) {
            int digit = num % 10;
            reversed = reversed * 10 + digit;
        }
        System.out.println("Reversed Number: " + reversed);
    }
}

Reverse a number in java using a for loop (User interaction)

The program will prompt a user to input the number and then it will reverse the same number using a for a loop.

import java.util.Scanner;
class ForLoopReverseDemo
{
   public static void main(String args[])
   {
      int num=0;
      int reversenum =0;
      System.out.println("Input your number and press enter: ");
      Scanner in = new Scanner(System.in);
      num = in.nextInt();
      for( ;num != 0; )
      {
          reversenum = reversenum * 10;
          reversenum = reversenum + num%10;
          num = num/10;
      }

      System.out.println("Reverse of specified number is: "+reversenum);
   }
}

Reverse a number in java using recursion (Without user interaction)

This is the program when the number already initialized. In this Program, you just need to change the value of the variable num as per your requirement.

class ReverseNumberDemo{ 
   public static void main(String args[])
   {
      int num=123456789;
      int reversenum =0;
      while( num != 0 )
      {
          reversenum = reversenum * 10;
          reversenum = reversenum + num%10;
          num = num/10;
      }

      System.out.println("Reverse of specified number is: "+reversenum);
   }
}

Reverse a number in java using recursion (User interaction)

The program will prompt a user to input the number and then it will reverse the same number using recursion.

import java.util.Scanner;
class RecursionReverseDemo
{
   public static void reverseMethod(int number) {
       if (number < 10) {
	   System.out.println(number);
	   return;
       }
       else {
           System.out.print(number % 10);
           reverseMethod(number/10);
       }
   }
   public static void main(String args[])
   {
	int num=0;
	System.out.println("Input your number and press enter: ");
	Scanner in = new Scanner(System.in);
	num = in.nextInt();
	System.out.print("Reverse of the input number is:");
	reverseMethod(num);
	System.out.println();
   }
}

If you have any queries please let us know.

Check it out Off Campus Jobs Updates: Click here

Technical Interview Questions of Company: Click here

HR Interview Questions of Company: Click here

How To Write Resume For Getting Shortlisted In Company: Click here

If You Want To Get More Daily Such Jobs Updates, Career Advice Then Join the Telegram Group From Given Link And Never Miss Update.

Join Telegram Group of Daily Jobs Updates for 2010-2021 Batch: Click Here

Why You’re Not Getting Response From Recruiter?: Click here

How To Get a Job Easily: Professional Advice For Job Seekers: Click here

Cognizant Latest News: Up To 20K+ Employees Will Be Hired: Click here

COVID-19 Live Tracker India & Coronavirus Live Update: Click here

Why Remove China Apps taken down from Play store?: Click here

Feel Like Demotivated? Check Out our Motivation For You: Click here

List of Best Sites To Watch Free Movies Online in 2020: Click here

5 Proven Tips For How To Look Beautiful and Attractive: Click here