Reverse an integer java: In the previous article, we have seen Java Program to Test if a double Number is an Integer
In this article we are going to see how to reverse a number using java programming language.
Java Program to Reverse an Integer Number
How to reverse an int in java: While reversing a number last numbers will come to first. See below examples to understand it clearly.
Example: Number: 12343 Then Reverse: 34321 Number 891 Then Reverse: 189
Let’s see different ways to reverse a number.
Method-1: Java Program to Reverse an Integer Number By Using Static Input Value
Approach:
- Declare an int variable say ‘
num
‘ and assign value to it, which needs to be reversed. - Declare a variable
rev = 0
to store the reversed number. - Run a while loop until the given number becomes zero.
- Inside the while loop, extract the last digit using %(modulo) operator (num % 10) and add it to 10 times the value of rev.
- Remove the last digit from the number by dividing it with 10.
- Print the number outside the loop
Program:
public class Main { public static void main(String[] args) { //an integer number declared int num = 12345; //intger variable 'rev' to store reverse value int rev = 0; //find reverse using while loop while (num != 0) { rev = rev * 10 + num % 10; num = num / 10; } System.out.println("Reversed num: " + rev); } }
Output: Reversed num: 54321
Method-2: Java Program to Reverse an Integer Number By Using User Input Value
Approach:
- Declare an int variable say ‘
num
‘ and take value of it as user input, which needs to be reversed. - Declare a variable
rev = 0
to store the reversed number. - Run a while loop until the given number becomes zero.
- Inside the while loop, extract the last digit using %(modulo) operator (num % 10) and add it to 10 times the value of rev.
- Remove the last digit from the number by dividing it with 10.
- Print the number outside the loop
Program:
import java.util.*; public class Main { public static void main(String[] args) { //Scanner class object craeted Scanner sc=new Scanner(System.in); //taking user input of a Number System.out.print("Enter a number: "); int num = sc.nextInt(); //intger variable 'rev' to store reverse value int rev = 0; //find reverse using while loop while (num != 0) { rev = rev * 10 + num % 10; num = num / 10; } System.out.println("Reversed number: " + rev); } }
Output: Enter a number: 4567 Reversed number: 7654
Method-3: Java Program to Reverse an Integer Number By Using User Defined Method
Approach:
- Declare an int variable say ‘
num
‘ and take value of it as user input, which needs to be reversed. - Then call a user defined method
reverse()
and pass thatnum
as argument. - Inside method declare a variable
rev = 0
to store the reversed number and run a while loop until the given number becomes zero. - Inside the while loop, extract the last digit using %(modulo) operator (num % 10) and add it to 10 times the value of rev.
- Remove the last digit from the number by dividing it with 10.
- Print the number outside the loop
Program:
import java.util.*; public class Main { public static void main(String[] args) { //Scanner class object craeted Scanner sc=new Scanner(System.in); //taking user input of a Number System.out.print("Enter a number: "); int num = sc.nextInt(); //callling reverse() method reverse(num); } //user defined method reverse() to find reverse public static void reverse(int num) { //intger variable 'rev' to store reverse value int rev = 0; //find reverse using while loop while (num != 0) { rev = rev * 10 + num % 10; num = num / 10; } System.out.println("Reversed number: " + rev); } }
Output: Enter a number: 876 Reversed number: 678
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs: