Friday, 18 December 2015

Java program to reverse the number

Requirement: When I run the Java program, it should ask me to enter numeric value. And as an output it should display the supplied numeric value in the reverse order (say if the supplied numeric value is 12345 it should give the output as 54321).

Solution: The Java program for the above requirement is shown below:

Program 1:
package com.susanto;

import java.util.Scanner; package com.susanto;

import java.util.Scanner;

public class ReverseNumber {
       public static void main(String args[]) {
             int n, reverse = 0;
             System.out.print("Enter the number to reverse : ");
             Scanner in = new Scanner(System.in);
             n = in.nextInt();
             while (n != 0) {
                    reverse = reverse * 10;
                    reverse = reverse + n % 10;
                    n = n / 10;
             }
             System.out.println("Reverse of entered number is : " + reverse);
       }
}


Output:
Enter the number to reverse : 12345
Reverse of entered number is : 54321
 


Thanks & Regards,
Susanto Paul