Knowee
Questions
Features
Study Tools

Write a Java program to check whether a given word is a palindrome or not. for e.g. MADAM is a palindrome but eat is not.

Question

Write a Java program to check whether a given word is a palindrome or not. for e.g. MADAM is a palindrome but eat is not.

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Sure, here is a simple Java program that checks if a given word is a palindrome or not:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a word:");
        String word = scanner.nextLine();
        String reversedWord = new StringBuilder(word).reverse().toString();
        if (word.equalsIgnoreCase(reversedWord)) {
            System.out.println(word + " is a palindrome.");
        } else {
            System.out.println(word + " is not a palindrome.");
        }
    }
}

Here's how it works:

  1. We import the Scanner class which is used to get user input.
  2. We create a new instance of the Scanner class.
  3. We prompt the user to enter a word.
  4. We read the user's input using the nextLine() method of the Scanner instance.
  5. We create a new StringBuilder instance with the user's word, reverse it, and convert it back to a String.
  6. We compare the reversed word with the original word using the equalsIgnoreCase() method. This method returns true if the two strings are equal, ignoring case considerations.
  7. If the original word and the reversed word are equal, we print that the word is a palindrome. Otherwise, we print that the word is not a palindrome.

This problem has been solved

Solution 2

Sure, here is a simple Java program that checks if a given word is a palindrome or not:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a word: ");
        String word = scanner.nextLine();
        String reversedWord = new StringBuilder(word).reverse().toString();
        if(word.equalsIgnoreCase(reversedWord)) {
            System.out.println(word + " is a palindrome.");
        } else {
            System.out.println(word + " is not a palindrome.");
        }
    }
}

Here's how it works:

  1. We import the Scanner class which is used to get user input.
  2. We create a new instance of the Scanner class.
  3. We ask the user to enter a word.
  4. We store the user's input in the word variable.
  5. We reverse the word using the StringBuilder class and its reverse method, and convert it back to a string.
  6. We compare the original word with the reversed word using the equalsIgnoreCase method. This method returns true if the two strings are equal, ignoring case considerations.
  7. If the original word and the reversed word are equal, we print that the word is a palindrome. Otherwise, we print that the word is not a palindrome.

This problem has been solved

Solution 3

Sure, here is a simple Java program that checks if a given word is a palindrome or not:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a word: ");
        String word = scanner.nextLine();
        String reversedWord = new StringBuilder(word).reverse().toString();
        if(word.equalsIgnoreCase(reversedWord)) {
            System.out.println(word + " is a palindrome.");
        } else {
            System.out.println(word + " is not a palindrome.");
        }
    }
}

Here's how it works:

  1. We import the Scanner class which is used to get user input.
  2. We create a new instance of the Scanner class.
  3. We prompt the user to enter a word.
  4. We read the user's input using the nextLine() method of the Scanner instance.
  5. We create a new StringBuilder instance with the user's word, reverse it and convert it back to a string.
  6. We compare the reversed word with the original word using the equalsIgnoreCase() method which ignores the case when comparing strings.
  7. If the original word and the reversed word are equal, we print that the word is a palindrome. Otherwise, we print that the word is not a palindrome.

This problem has been solved

Similar Questions

class Solution { public boolean isPalindrome(int x) { String num =Integer.toString(x); for(int i=num.length()-1;i<0;i--){ String a =" "+num; if(num==a){ System.out.println("it is palindrome:"+a); }else{ System.out.println("it is not palindrome:"+a); } } }}

Write a python program to check the given string is palindrome or not.

With the Module on  Java Characters, Strings, and the StringBuilder that I have provided in our Messenger GC and in Teams. Do this Laboratory Activity:Write a Java program that takes a user input string, reverses it, and then checks if it is a palindrome. Sample Words to check: civic, radar, level, rotor, kayak, madam, and refer

You are provided with a code to find if a given string is a palindrome or not.The code is shuffled.  Rearrange the code in the correct order.System.out.println(str+" is not a Palindrome");import java.util.Scanner;StringBuffer sb = new StringBuffer(str);public static void main(String[] args) {Scanner sc=new Scanner(System.in);public class Main {elseString str=sc.next();sb.reverse();if(str.equalsIgnoreCase(reverse))   }}System.out.println(str+" is a Palindrome");String reverse=sb.toString();

Write a program in java to accept a number and chech whether it is a palindrome number or not. Do the program without using modulus operator and String.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.