From 8cc258a66a671567877d34791f524892ae6cd65b Mon Sep 17 00:00:00 2001 From: prit1199 <92705386+prit1199@users.noreply.github.com> Date: Fri, 16 Feb 2024 12:24:47 +0530 Subject: [PATCH 1/2] Update Exercise06_03.java update reverse ( ) method, replace String variable with int. --- ch_06/Exercise06_03.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/ch_06/Exercise06_03.java b/ch_06/Exercise06_03.java index a00bf63..7f3c12f 100644 --- a/ch_06/Exercise06_03.java +++ b/ch_06/Exercise06_03.java @@ -26,15 +26,14 @@ public static void main(String[] args) { } } - public static int reverse(int number) { - String temp = ""; - while (number > 0) { - int t = number % 10; - number /= 10; - temp += t; - - } - return Integer.parseInt(temp); + public static int reverse(int n) { + int num1 = 0; + while( n != 0){ + num1 =num1 *10; + num1 += n % 10 ; + n = n / 10; + } + return num1; } public static boolean isPalindrome(int number) { @@ -44,4 +43,4 @@ public static boolean isPalindrome(int number) { return n1.equals(n2); } -} \ No newline at end of file +} From 1656151035549a10669226d7064c034bd2f867c2 Mon Sep 17 00:00:00 2001 From: prit1199 <92705386+prit1199@users.noreply.github.com> Date: Fri, 16 Feb 2024 12:38:49 +0530 Subject: [PATCH 2/2] Update Exercise06_03.java --- ch_06/Exercise06_03.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/ch_06/Exercise06_03.java b/ch_06/Exercise06_03.java index 7f3c12f..7668cf3 100644 --- a/ch_06/Exercise06_03.java +++ b/ch_06/Exercise06_03.java @@ -26,21 +26,18 @@ public static void main(String[] args) { } } - public static int reverse(int n) { - int num1 = 0; + public static int reverse(int number) { + int reverseNum = 0; while( n != 0){ - num1 =num1 *10; - num1 += n % 10 ; - n = n / 10; + reverseNum = reverseNum *10; + reverseNum += number % 10 ; + number = number / 10; } - return num1; + return reverseNum; } public static boolean isPalindrome(int number) { - String n1 = String.valueOf(number); - String n2 = String.valueOf(reverse(number)); - - return n1.equals(n2); - + return ( number == reverse(number)) } + }