Java Program to Reverse a Number

[2682 views]




In this article, we will write a Simple Java Program which reverses the number. Ex., 243 gets reversed to 342.

Reversing Number Code in Java:

import java.util.Scanner; class NumberReverse { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Get input from the user System.out.println("Enter the number you want to reverse"); int number = sc.nextInt(); //Use a variable and initialize it to zero in which we will store reverse number int reverse = 0; //loop until the number becomes zero while(number != 0) { //multiply the reverse number with 10 to shift its digit to next place i.e 2 becomes 20 reverse = reverse * 10; //add the remainder(number%10 gives remainder) of number/10 to the reverse number reverse = reverse + number%10; //divide the number by 10 to reduce its one place i.e 243 becomes 24 number = number/10; } //print the reverse number System.out.println("The reverse number is "+reverse); } }

Output:

Java Program to Reverse a Number using Modulus
                 



Clear Any Java Interview by Reading our Ebook Once.


Can you clear Java Interview?




Comments










Search Anything:

Sponsored Deals ends in





Technical Quizzes Specially For You:

Search Tags

    Reverse a Number using a while loop in Java

    Write a program to Reverse a number using Java