A palindrome is a word, number, or sequence that reads the same forwards and backwards. Examples: 'MADAM', 'RACECAR', 12321. Writing a palindrome program is a standard Class 11-12 Computer Science Java exercise.
Palindromes appear in interview coding questions, competitive programming (LeetCode, HackerRank), and CBSE Class 11-12 Computer Science practical exams.
import java.util.Scanner;
public class PalindromeString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev = rev + str.charAt(i);
}
if (str.equalsIgnoreCase(rev)) {
System.out.println(str + " is a Palindrome");
} else {
System.out.println(str + " is NOT a Palindrome");
}
}
}
Output: Enter a string: MADAM โ MADAM is a Palindrome
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int original = n, rev = 0, rem;
while (n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
if (original == rev) {
System.out.println(original + " is a Palindrome");
} else {
System.out.println(original + " is NOT a Palindrome");
}
}
}
Output: Enter a number: 121 โ 121 is a Palindrome
Repeatedly take the last digit using (n % 10), add it to rev*10, then remove the last digit using (n / 10). Repeat until n becomes 0.
What are Output Devices? (Definition and Examples)
Learn the definition of computer output devices. Find clear examples like monitors, printers, speakers, and projectors, explained for students.
What is a Web Browser? (With Examples)
Learn what a web browser is and see popular examples of web browsers like Google Chrome, Safari, Mozilla Firefox, and Microsoft Edge.
Fibonacci Series in C (Code and Logic)
Learn how to write a Fibonacci series program in C. Understand the logic with both iterative and recursive methods with complete code examples.
What is Firewall Authentication?
Learn what firewall authentication is in computer networks. Understand how it blocks unauthorized hackers while allowing verified users to access internal company data.
Floor Division in Python ( // Operator)
Learn how floor division works in Python using the // operator. Understand how it rounds down the result to the nearest integer with examples.
Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast โ free, no signup required.