An integer is a palindrome when you read from the forward and read from the backward are same. or numeric palindrome, A number that remains the same when its digits are reversed.

For Example: 121 is palindrome as from backward it also 121.

313 is number palindrome.

1001 is palindromic number.

A String palindrome is same as integer, where a string that remain the same when its string are reversed.

For Example: MALAYALAM, AMANAPLANACANALPANAMA etc are string palindrome

Problem 1

Write a java code for the Integer Palindrome.

Code

import java.util.Scanner;

public class IntegerPalindrome {
	
	public static boolean palin(Integer num) {
		String revstr = new StringBuilder(num.toString()).reverse().toString();

		if(num.toString().equalsIgnoreCase(revstr)) {
			return true;
		}
		return false;		
	}
	
	public static void main(String[] args) {
		System.out.println("Enter number");
        Scanner scanner = new Scanner(System.in);
        Integer cnt = scanner.nextInt();        
        scanner.close();
        
        System.out.print(IntegerPalindrome.palin(cnt)? "Palindrome" : "Not Palindrome");
	}

}

Run Code

Problem 2

Write a string palindrome code in java.

Code

import java.util.Scanner;

public class Palindrome {
	
	public static boolean palin(String str) {
		String revstr = new StringBuilder(str).reverse().toString();
		

		if(str.equalsIgnoreCase(revstr)) {
			return true;
		}
		return false;		
	}
	
	public static void main(String[] args) {
		System.out.println("Enter string ");
        Scanner scanner = new Scanner(System.in);
        String cnt = scanner.next();        
        scanner.close();
        
        System.out.print(Palindrome.palin(cnt)? "Palindrome" : "Not Palindrome");
	}

}

Run Code

Similar Posts