The following code example demonstrates how to Form a Palindrome by Removing One Digit in Java.

When a number is the same as its reverse, it is a palindrome. In the following program, first, check whether the given number is a palindrome or not. If it is a palindrome, then return -1. Otherwise, remove a digit from the number to make it a palindrome and return that digit. It is assumed that the number has only one digit that can be adjusted to make it a palindrome,

For example, let the number=12421, then return -1 since the number is a palindrome.

When the number = 234382, return 8. Because, if we remove 8 from the number, the number will become palindrome.

Similarly, when number= 91221, return 9.

Also, if the number = 781876, return 6.

public class FormPalindrome
{
	public static void main(String[] args)
	{
		System.out.println(formAPalindrome(381483));
		System.out.println(formAPalindrome(12121));
		System.out.println(formAPalindrome(98118));
		System.out.println(formAPalindrome(251532));
	}
	public static int formAPalindrome(int my_num)
	{
		String str, str1, str2="";
		str=Integer.toString(my_num);
		StringBuilder sb=new StringBuilder(str);
		str1=sb.reverse().toString();
		if(str.equals(str1))
		{
			return -1;
		}
		else
		{
			int num=0;
			for(int i=0;i<str.length();i++)
			{
				str2=str.substring(0, i)+str.substring(i+1);
				sb=new StringBuilder(str2);
				str1=sb.reverse().toString();
				if(str2.equals(str1))
				{
					num=str.charAt(i)-'0';
					break;
				}
			}
			return num;
		}			
	}
}

Output

A Program to Form a Palindrome by Removing One Digit in Java
A Program to Form a Palindrome by Removing One Digit in Java

Further Reading

Java Practice Exercise

programmingempire

Princites