https://www.facebook.com/codewithredoy
xxxxxxxxxx
// Follow me: www.facebook.com/codewithredoy
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number - ");
int num = s.nextInt();
String number = String.valueOf(num);
String reverse = new StringBuilder(number).reverse().toString();
if (number.equals(reverse)) {
System.out.println(num + " is a Palindrome number.");
}
else {
System.out.println(num + " isn't a palindrome number.");
}
}
}
xxxxxxxxxx
class Solution {
public boolean isPalindrome(int x) {
if(x < 0)
return false;
return x == reverseNum(x);
}
private static int reverseNum(int num){
int rev = 0;
while(num>0){
int digit = num%10;
rev = (rev * 10) + digit;
num = num/10;
}
return rev;
}
}
xxxxxxxxxx
public static void main(String[] args) throws ParseException {
String word;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
word = in.nextLine();
System.out.println(palindromeChecker(word));
}
public static String palindromeChecker(String word){
int length = word.length();
String reverse="";
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + word.charAt(i);
if (word.equals(reverse))
return "Palindrome";
else
return "Not a Palindrome";
}
xxxxxxxxxx
public static boolean checkPalindrome(String str)
{
int len = str.length();
for(int i = 0; i < len / 2; i++) {
if (str.charAt(i) != str.charAt(len - i - 1))
return false;
}
return true;
}
xxxxxxxxxx
public boolean isPalindrome(int x) {
String str = Integer.toString(x);
String reverse = new StringBuilder(str).reverse().toString();
return str.equals(reverse)?true:false;}
xxxxxxxxxx
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
static boolean isPalindrome(String s)
{
/* Two pointers initialised to 0 and 's.length()-1'.*/
int i = 0, j = s.length() - 1;
/* Looping through the string */
while (i < j) {
/* Even if one character is not equal, we will return false. */
if (s.charAt(i) != s.charAt(j))
return false;
/* Value of pointers is changed */
i++;
j--;
}
/* This means all characters are same and thus string is a palindrome*/
return true;
}
public static void main(String[] args)
{
String s = "abba";
/* First, We'll convert it to lower case. */
s = s.toLowerCase();
if (isPalindrome(s))
System.out.print("Yes");
else
System.out.print("No");
}
}
xxxxxxxxxx
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int temp = num;
int rev = 0;
while (num > 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
System.out.println(temp==rev?"palindrome":"not palindrome");
}
xxxxxxxxxx
public boolean isPalindrome(int x) {
String str = Integer.toString(x);
String reverse = new StringBuilder(str).reverse().toString();
return str.equals(reverse)?true:false;
}
xxxxxxxxxx
package com.howtodoinjava.puzzle;
public class PalindromeTest
{
/**
* Test the actual code if it works correctly
* */
public static void main(String[] args)
{
System.out.println(checkIntegerPalindrome( 100 )); //false
System.out.println(checkIntegerPalindrome( 101 )); //true
System.out.println(checkIntegerPalindrome( 500045 )); //false
System.out.println(checkIntegerPalindrome( 50005 )); //true
}
/**
* This function will test the equality if a number and its reverse.
* @return true if number is palindrome else false
* */
public static boolean checkIntegerPalindrome(int number)
{
boolean isPalindrome = false;
if(number == reverse(number))
{
isPalindrome = true;
}
return isPalindrome;
}
/**
* This function will reverse a given number.
* @return reverse number
* */
public static int reverse(int number)
{
int reverse = 0;
int remainder = 0;
do {
remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number / 10;
} while (number > 0);
return reverse;
}
}
Program Output.
false
true
false
true
xxxxxxxxxx
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static boolean isPalindrome(String str)
{
// Initializing an empty string to store the reverse
// of the original str
String rev = "";
// Initializing a new boolean variable for the
// answer
boolean ans = false;
for (int i = str.length() - 1; i >= 0; i--) {
rev = rev + str.charAt(i);
}
// Checking if both the strings are equal
if (str.equals(rev)) {
ans = true;
}
return ans;
}
public static void main(String[] args)
{
// Input string
String str = "geeks";
// Convert the string to lowercase
str = str.toLowerCase();
boolean A = isPalindrome(str);
System.out.println(A);
}
}