Java Program to Reverse a Number


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
        


Need Programming or any Technical Related Books, Courses or Help Chat with us

Comments



Deals of the Day
Search
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online


Search Tags

    Reverse a Number using a while loop in Java

    Write a program to Reverse a number using Java