class Solution { public boolean isPalindrome(int x) { String num =Integer.toString(x); for(int i=num.length()-1;i<0;i--){ String a =" "+num; if(num==a){ System.out.println("it is palindrome:"+a); }else{ System.out.println("it is not palindrome:"+a); } } }}
Question
class Solution { public boolean isPalindrome(int x) { String num =Integer.toString(x); for(int i=num.length()-1;i<0;i--){ String a =" "+num; if(num==a){ System.out.println("it is palindrome:"+a); }else{ System.out.println("it is not palindrome:"+a); } } }}
Solution
The code you've written is in Java and it's supposed to check if a given integer is a palindrome. However, there are a few issues with it. Here's a step-by-step explanation:
-
You're converting the integer
xto a stringnum. This is fine. -
You're running a loop from the end of the string to the beginning. However, the condition in your loop is incorrect. It should be
i >= 0instead ofi < 0. -
Inside the loop, you're creating a new string
awhich is justnumwith a space in front. This doesn't make sense if you're trying to check for a palindrome. -
You're then comparing
numandawith==. In Java,==checks for reference equality for objects, not their contents. You should use.equals()to compare strings. -
You're printing out whether the number is a palindrome inside the loop. This should be done after the loop.
Here's a corrected version of your code:
class Solution {
public boolean isPalindrome(int x) {
String num = Integer.toString(x);
int i = 0, j = num.length() - 1;
while (i < j) {
if (num.charAt(i) != num.charAt(j)) {
System.out.println("It is not a palindrome: " + num);
return false;
}
i++;
j--;
}
System.out.println("It is a palindrome: " + num);
return true;
}
}
This code converts the integer to a string, then checks if it's a palindrome by comparing the characters at the start and end of the string, moving towards the center. If it finds any pair of characters that don't match, it prints out a message and returns false. If it gets through the whole string without finding any mismatched characters, it prints out a message and returns true.
Similar Questions
You are provided with a code to find if a given string is a palindrome or not.The code is shuffled. Rearrange the code in the correct order.System.out.println(str+" is not a Palindrome");import java.util.Scanner;StringBuffer sb = new StringBuffer(str);public static void main(String[] args) {Scanner sc=new Scanner(System.in);public class Main {elseString str=sc.next();sb.reverse();if(str.equalsIgnoreCase(reverse)) }}System.out.println(str+" is a Palindrome");String reverse=sb.toString();
Given an integer x, return true if x is a palindrome, and false otherwise.
Given an integer x, return true if x is a palindrome, and false otherwise. Example 1:Input: x = 121Output: trueExplanation: 121 reads as 121 from left to right and from right to left.Example 2:Input: x = -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.Example 3:Input: x = 10Output: falseExplanation: Reads 01 from right to left. Therefore it is not a palindrome.
Write a Java program to check whether a given word is a palindrome or not. for e.g. MADAM is a palindrome but eat is not.
Write a program in java to accept a number and chech whether it is a palindrome number or not. Do the program without using modulus operator and String.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.