codescracker


java

Java Program to Reverse Number



« Previous Program Next Program »


Reverse Number

To reverse a number in Java Programming, you have to ask to the user to enter the number. Start reversing the number, first make a variable rev and place 0 to rev initially, and make one more variable say rem. Now place the modulus of the number to rem and place rev*10+rem to the variable rev, now divide the number with 10 and continue until the number will become 0.

Java Programming Code to Reverse Number

Following Java Program ask to the user to enter any number to find its reverse, then display the result on the screen :

/* Java Program Example - Reverse a Number */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int num, rev=0, rem;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Number : ");
        num = scan.nextInt();
		
        while(num != 0)
        {
            rem = num%10;
            rev = rev*10 + rem;
            num = num/10;
        }
		
        System.out.print("Reverse = " +rev);
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program to reverse number

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test