Pages

How to check given number is palindrome?

 Java palindrome program ?

>> In Java we can verify given number, string is palindrome or not.

>> Palindrome is a number/string , it should be same  after reverse the number/string.

public class palindrome {
public static void main(String[] args) {
int num = 121, rem;
int temp = num;
int sum = 0;
while (num > 0) {
rem = num % 10;
sum = (sum * 10) + rem;
num = num / 10;
}
if (temp == sum) {
System.out.println("Given number " + temp + " is palindrome");
}

System.out.println(
"<<------------------------------------------>>");
String str = "WOOOOOW";
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev = rev + str.charAt(i);
}
System.out.println("reverse string using for loop=" + rev);
if (str.equals(rev)) {
System.out.println("Given string " + str + " is palindrome");
}

}
}
Output:
Java palindrome
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.