- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
Iterative Constructs in Java
Chapter 9
Iterative Constructs in Java
Class 10 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Java Program: Menu Driven Program - Palindrome and Perfect Number Checker
31. Write a menu driven class to accept a number from the user and check whether it is Palindrome or a Perfect number.
(1). Palindrome number: A number is a Palindrome which when read in reverse order is same as read in the right order Example: 11, 101, 151, etc.
(2). Perfect number: A number is called Perfect if it is equal to the sum of its factors other than the number itself.
Example: 6 = 1 + 2 + 3
Program Title: Palindrome and Perfect Number Checker
import java.util.Scanner;
public class NumberChecker {
// Method to check if a number is Palindrome
public static boolean isPalindrome(int num) {
int original = num;
int reverse = 0;
while (num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}
return original == reverse;
}
// Method to check if a number is Perfect
public static boolean isPerfect(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) { // only factors less than num
if (num % i == 0) {
sum += i;
}
}
return sum == num;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Menu:");
System.out.println("1. Check Palindrome");
System.out.println("2. Check Perfect Number");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
System.out.print("Enter a number: ");
int number = sc.nextInt();
switch (choice) {
case 1:
if (isPalindrome(number)) {
System.out.println(number + " is a Palindrome number.");
} else {
System.out.println(number + " is not a Palindrome number.");
}
break;
case 2:
if (isPerfect(number)) {
System.out.println(number + " is a Perfect number.");
} else {
System.out.println(number + " is not a Perfect number.");
}
break;
default:
System.out.println("Invalid choice! Please select 1 or 2.");
}
sc.close();
}
}
Output
Sample Output 1 (Palindrome) Menu: 1. Check Palindrome 2. Check Perfect Number Enter your choice: 1 Enter a number: 121 121 is a Palindrome number. Sample Output 2 (Perfect Number) Menu: 1. Check Palindrome 2. Check Perfect Number Enter your choice: 2 Enter a number: 28 28 is a Perfect number.
How It Works
- Palindrome Check: Reverse the number and compare with original.
- Perfect Number Check: Sum all factors less than the number and compare with the number.
- Menu Driven: Switch-case to choose between the two options.
