integer palindrome in java code example
Example 1: generate palindrome java
public static String generatePalindrom(String s) {
return s.equals(reverseString(s)) ? s
: generatePalindrom(String.valueOf(Integer.parseInt(s) + Integer.parseInt(reverseString(s))));
}
public static String reverseString(String s) {
return s.length() == 1 ? s : s.charAt(s.length() - 1) + reverseString(s.substring(0, s.length() - 1));
}
Example 2: easy palindrome program in java
import java.util.*;
class PalindromeExample2
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string/number to check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string/number is a palindrome.");
else
System.out.println("Entered string/number isn't a palindrome.");
}
}
Example 3: palindrome number in java
import java.util.Scanner;
public class Palindrome
{
public static void main(String args[])
{
int num,temp,reverse=0;
Scanner input=new Scanner(System.in);
num=in.nextInt();
temp=num;
while(temp!=0)
{
int d=temp%10;
reverse=reverse*10+d;
temp/=10;
}
if(reverse==num)
{
System.out.println("Number is palindrome");
}
else
{
System.out.println("Number is not palindrome");
}
}
}
Example 4: palindrome in java
package test
public boolean isPalindromString(String text){
String reverse = reverse(text);
if(text.equals(reverse))
{
return true;
}
return false;
}
public String reverse(String input)
{
if(input == null || input.isEmpty())
{
return input;
}
return input.charAt(input.length()- 1) + reverse
(input.substring(0, input.length() - 1));
}
Example 5: integer palindrome in java
public boolean isPalindrome(int x) {
String str = Integer.toString(x);
String reverse = new StringBuilder(str).reverse().toString();
return str.equals(reverse)?true:false;
}
Example 6: integer palindrome in java
public boolean isPalindrome(int x) {
String str = Integer.toString(x);
String reverse = new StringBuilder(str).reverse().toString();
return str.equals(reverse)?true:false;}