Java Program to find Reverse of Number

Ā 
Video Tutorials
Artificial Intelligence Tutorial
Machine Learning Projects TutorialPython Video tutorial
Computer Graphics Video TutorialC++ Video Tutorial
Big Data Analytics Video TutorialSoft Computing Video Tutorial
Placement Video TutorialJava Video Tutorial

Write a Java Program to find Reverse of Number – Java Tutorial

Problem Statement

 Write a Java program to find the reverse of a number.

Solution:

First, create a class named Reverse and add the main method to it.

Next, set the value of rev to 0. Read the number to be reversed from the user using an object of scanner class object.

Video Tutorial:

Repeat the following three steps repeatedly until the value of num is greater than 0.

1. rem = num % 10

2. rev = rev*10 + rem

3. num = num / 10

Finally, print the value of Reverse using the statement System.out.println

import java.util.Scanner;

/*
 * Write a Java Program to find the reverse of a given number
 *  
 * Subscribe to Mahesh Huddar YouTube Channel for more Videos
 * 
 * rev = 0
 * num = 123
 * 1. rem = num % 10          3       2       1
 * 2. rev = rev*10 + rem      3       32      321
 * 3. num = num / 10          12      1       0
 */

public class Reverse {

	public static void main(String[] args) 
	{
		int rev = 0, num, org, rem;
		Scanner in = new Scanner(System.in);
		System.out.println("Enter a number:");
		num = in.nextInt();
		org = num;
		while(num>0)
		{
			rem = num % 10;
			rev = rev*10 + rem;
			num = num / 10;
		}
		System.out.println("The reverse of " + org + " is "+ rev);
	}
}
 

The output of the Factorial Program in Java

Enter a number: 123

The reverse of 123 is 321

Summary:

In this article, we understood How to write a Java Program to get the Reverse of a Number – Java Tutorial. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.

Leave a Comment

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