ICSE Class 10 Computer Applications Question 43 of 45

Iterative Constructs in Java — Question 44

Back to all questions
44
Question

Question 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
import java.util.Scanner;

public class KboatPalinOrPerfect
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("1. Palindrome number");
        System.out.println("2. Perfect number");
        System.out.print("Enter your choice: ");
        int choice = in.nextInt();
        System.out.print("Enter number: ");
        int num = in.nextInt();

        switch (choice) {
            case 1:
            int copyNum = num;
            int revNum = 0;

            while(copyNum != 0) {
                int digit = copyNum % 10;
                copyNum /= 10;
                revNum = revNum * 10 + digit;
            }

            if (revNum == num) 
                System.out.println(num + " is palindrome");
            else
                System.out.println(num + " is not palindrome");
            break;

            case 2:
            int sum = 0;

            for (int i = 1; i <= num / 2; i++) {
                if (num % i == 0) {
                    sum += i;
                }
            }

            if (num == sum) 
                System.out.println(num + " is a perfect number");
            else
                System.out.println(num + " is not a perfect number");
            break;

            default:
            System.out.println("Incorrect Choice");
            break;
        }
    }
}
Output
BlueJ output of KboatPalinOrPerfect.java
BlueJ output of KboatPalinOrPerfect.java
Answer

Source: This question is from Iterative Constructs in Java, Computer Applications — Class 10, ICSE Board.

Key Concepts Covered

This question tests your understanding of the following concepts from the chapter Iterative Constructs in Java: Question, Menu, Driven, Class, Accept, Number. These are fundamental topics in Computer Applications that students are expected to master as part of the ICSE Class 10 curriculum.

A thorough understanding of these concepts will help you answer similar questions confidently in your ICSE examinations. These topics are frequently tested in both objective and subjective sections of Computer Applications papers. We recommend revising the relevant section of your textbook alongside practising these solved examples to build a strong foundation.

How to Approach This Question

Read the question carefully and identify what is being asked. Break down complex questions into smaller parts. Use the terminology and concepts discussed in this chapter. Structure your answer logically — begin with a definition or key statement, then provide supporting details. Review your answer to ensure it addresses all parts of the question completely.

Key Points to Remember

  • Write programs with proper indentation and comments.
  • Trace through your code with sample inputs to verify correctness.
  • Explain the logic behind each step of your solution.
  • Familiarise yourself with common library functions and methods.

Practice more questions from Iterative Constructs in Java — Computer Applications, Class 10 ICSE