Tuesday, 5 February 2019

11. Palindrome program in java.


Description:
Write a sample code to Palindrome in java.


Code:
?
1
2
3
4
5
6
7
8
9
import java.util.Scanner; class Palindrome { public static void main(String args[]) { String original, reverse = ""; Scanner in = new Scanner(System.in); System.out.println("Enter a string to check :"); 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("The string is a palindrome."); else System.out.println("The string isn't a palindrome."); } }

Output:
Enter a string to check :
madam
The string is a palindrome.

Enter a string to check :
godblessgovind
The string isn't a palindrome.

No comments:

Post a Comment