0% found this document useful (0 votes)
62 views

Palindrome in Java

This Java program accepts a string from the user, converts it to a character array, and then checks if the first and last characters are equal, then second and second last, and so on. If all pairs are equal, it prints that the string is a palindrome, otherwise it is not a palindrome. It uses a Scanner to get user input, initializes a boolean variable to track the palindrome status, and uses a for loop with indexes moving in opposite directions to compare corresponding characters from each end of the array.

Uploaded by

S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Palindrome in Java

This Java program accepts a string from the user, converts it to a character array, and then checks if the first and last characters are equal, then second and second last, and so on. If all pairs are equal, it prints that the string is a palindrome, otherwise it is not a palindrome. It uses a Scanner to get user input, initializes a boolean variable to track the palindrome status, and uses a for loop with indexes moving in opposite directions to compare corresponding characters from each end of the array.

Uploaded by

S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

package palindrome;

import java.util.Scanner;

public class palindrome {

public static void main(String[] args) {

String a;

Scanner sc= new Scanner(System.in);

boolean b = true;

System.out.println("enter char : ");

a= sc.nextLine();

char str[]= a.toCharArray();

int n = str.length-1;

for (int i = 0, j=n; i <= n && j >=0 ; i++, j--)

if (str[i] == str[j])

b = true;

else

b = false;

break;

if(b)

System.out.println("it is a palindrome");

}
else{

System.out.println("it is not a palindrome");

You might also like