Q:

Write a Java program to reverse an integer number

0

Write a Java program to reverse an integer number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

public class Javamathexcercise {
   public static void main(String[] args) {
    int number =123456789;   
    int is_positive = 1;
        if (number < 0) {
            is_positive = -1;
            number = -1 * number;
        }
        int sum  = 0;
        while (number > 0) {
            int r = number % 10;
 
            int maxDiff = Integer.MAX_VALUE - sum * 10;
            if (sum > Integer.MAX_VALUE / 10 || r > maxDiff) 
                System.out.println("wrong number");;
 
            sum = sum * 10 + r;
            number /= 10;
        }
        System.out.println(is_positive * sum);
   }
}

Result:
987654321

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a Java program to round a float number to sp... >>