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.
Multiprogramming Operating System
Learn what a multiprogramming operating system is. Understand how it keeps the CPU busy by switching between multiple programs and its advantages.
Difference Between Primary and Secondary Memory
Learn the key differences between Primary Memory (RAM/ROM) and Secondary Memory (Hard Drives/SSD). Understand volatility, speed, and CPU access.
What is the Print Preview Shortcut Key?
Learn the keyboard shortcut key for Print Preview in Windows, Microsoft Word, Excel, and Web browsers like Chrome.
Find and Replace Shortcut Key (Windows & Mac)
Learn the keyboard shortcut for Find and Replace in MS Word, Excel, and Notepad. Use Ctrl+H for Windows and Cmd+Shift+H for Mac.
What is the 'static' Keyword in Java?
Learn about the static keyword in Java. Understand how static variables, methods, and blocks are used for memory management at the class level.
Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast โ free, no signup required.