
Asked in TCS and 26 others
Write a Java program to check if a given string is a palindrome (e.g., MOM).

Program to check if a given string is a palindrome in Java.
Create a function that takes a string as input.
Reverse the input string and compare it with the original string.
If they are the same, the str...read more

import java.util.*;
class PalindromeExample2
{
public static void main(String args[])
{
String original, reverse = ""; // Objects of String class
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.");
}
}
A palindrome number is a number that is same after reverse. For example 121, 34543, 343, 131, 48984 are the palindrome numbers. Palindrome number algorithm
Let's see the palindrome program in C. In this c program, we will get an input from the user and check whether number is palindrome or not. ADVERTISEMENT
ADVERTISEMENT
Output: enter the number=151 palindrome number enter the number=5621 not palindrome number |
- Send your Feedback to feedback@javatpoint.com
Interview Questions from Popular Companies










Reviews
Interviews
Salaries
Users

