Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

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

  1. Palindrome Check: Reverse the number and compare with original.
  2. Perfect Number Check: Sum all factors less than the number and compare with the number.
  3. Menu Driven: Switch-case to choose between the two options.