Output:
Click Here For Java Online Compiler
Solution:
*** Reverse An Integer *** Enter any number : 1234567 The Original Number is: 1234567 Reversed Number is : 7654321
Click Here For Java Online Compiler
Solution:
import java.util.Scanner; class ReverseAnInteger { public static void main(String[] args) { System.out.println("*** Reverse An Integer ***" + "\n"); Scanner scanner = new Scanner(System.in); System.out.print("Enter any number : "); int num = scanner.nextInt(); System.out.println("\nThe Original Number is: " + num); int is_positive = 1; if (num < 0) { is_positive = -1; num = -1 * num; } int sum = 0; while (num > 0) { int r = num % 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; num /= 10; } int reversed = is_positive * sum; System.out.println("Reversed Number is : " + reversed); } }
comment 0 comments:
more_vertsentiment_satisfied Emoticon