• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » Reverse Number In Java

Reverse Number In Java

Last Updated on: August 14, 2022 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Reverse Number In Java
  • Reverse Number In Java Using While Loop
  • Reverse Number In Java Using For Loop
  • Reverse Number In Java Using Recursion

In this tutorial, you’ll learn how to reverse a number in Java. You’ll see how to do it with different techniques and find out which one is the most efficient for your needs.

Post Type:Java Programs For Beginners
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:Join Our Telegram Group

Reverse Number In Java

We’ve talked about the different ways you can reverse a number in a Java program. The compiler is added to each and every program so it can be executed. Examples are given with accompanying sample outputs.

  • Using While Loop
  • Using For Loop
  • Using Static Method
  • Using Function
  • Using Recursion
Reverse Number In Java
Reverse Number In Java

Reverse Number In Java Using While Loop

This method will allow us to use a while loop to break down the number input and rearrange it in reverse order. We’ll be using the modulo operator to extract digits from the number, and the divide operator to shorten it.

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class ReverseNumberEx1 
{
	public static void main(String[] args) 
	{
		int num=0;
		int reversenum =0;
		System.out.println("Input your number and press enter: ");
		//This statement will capture the user input
		Scanner in = new Scanner(System.in);
		//Captured input would be stored in number num
		num = in.nextInt();
		//While Loop: Logic to find out the reverse number
		while( num != 0 )
		{
			reversenum = reversenum * 10;
			reversenum = reversenum + num%10;
			num = num/10;
		}

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

Output:

Input your number and press enter:  152
Reverse of input number is: 251

Reverse Number In Java Using For Loop

In this method, we will use a for loop instead of a while loop.

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class ReverseNumberEx2 
{
	public static void main(String[] args) 
	{
		int num=0;
		int reversenum =0;
		System.out.println("Input your number and press enter: ");
		//This statement will capture the user input
		Scanner in = new Scanner(System.in);
		//Captured input would be stored in number num
		num = in.nextInt();
		//While Loop: Logic to find out the reverse number
		
		//loop to find reverse number
        for( ;num != 0;)
        {
        	reversenum = reversenum * 10;
            reversenum = reversenum + num%10;
            num = num/10;
        };

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

Output:

Input your number and press enter: 159
Reverse of input number is: 951

Reverse Number In Java Using Recursion

This method divides a number by 10 and displays the remainder. It then calls itself, passing the quotient as a parameter. This process continues until the number is in single digits. The last digit (which is also the first digit of the original number) is displayed, and then the recursion ends.

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class ReverseNumberEx3 
{
	public static void main(String[] args) 
	{
		int num=0;
		int reversenum =0;
		System.out.println("Input your number and press enter: ");
		//This statement will capture the user input
		Scanner in = new Scanner(System.in);
		//Captured input would be stored in number num
		num = in.nextInt();
		System.out.println ("Reversed Number: " + getReverse(num, reversenum));
	}
	static int getReverse (int num, int rev)
	{
		if (num == 0)
			return rev;

		int rem = num % 10;
		rev = rev * 10 + rem;

		return getReverse (num / 10, rev);
	}
}

Output:

Input your number and press enter: 123
Reversed Number: 321

You can do the same regression using the same recursive function with a single parameter like below

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class ReverseNumberEx4 
{
	public static void main(String[] args) 
	{
		int num=0;
		int reversenum =0;
		System.out.println("Input your number and press enter: ");
		//This statement will capture the user input
		Scanner in = new Scanner(System.in);
		//Captured input would be stored in number num
		num = in.nextInt();
		getReverse(num);
	}
	static void getReverse (int num)
	{
		if (num == 0)
			return;

		int rem = num % 10;
		System.out.print (rem);

		getReverse (num / 10);
	}
}

Output:

Input your number and press enter: 789
987

Conclusion:

In this blog post, we have tried to share various possible ways to Reverse Number In Java. If you found its helpful, please share it with your friends and colleagues. If you have any questions or feedback, drop a comment below.

    WAP To Remove Consecutive Duplicates from Array
    WAP To Remove Consecutive Duplicates from Array
    WAP to Count Occurrence of Characters in a String
    WAP to Count Occurrence of Characters in a String
    Substring Matching In Java
    Substring Matching In Java
    WAP to Print the Character Based On their Number Of Occurance In Ascending Order
    Wap To Print Characters As Per Number Of Occurrences
    WAP to Replace Last Two Special Character With Dots
    WAP to Replace Last Two Special Character With Dots
    Find the Duplicate Words in a String & File
     Program to Find the Duplicate Words in a String &...
    WAP to Remove Digits Only From a String
    WAP to Remove Digits Only From a String
    WAP to Reverse Every Third Word In a String
    WAP to Reverse Every Third Word In a String
    WAP to Format a Phone Number in Human-Readable View
    WAP to Format a Phone Number in Human-Readable View
    WAP To Print Each Character 2 Times
    WAP To Print Each Character 2 Times Of Character Array

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers