ICSE Class 10 Computer Applications Question 29 of 45

Iterative Constructs in Java — Question 30

Back to all questions
30
Question

Question 26

Write a menu driven program to accept a number and check and display whether (i) it is a Prime Number or not (ii) it is an Automorphic Number or not. (Use switch-case statement).

  1. Prime number: A number is said to be a prime number if it is divisible only by 1 and itself and not by any other number. Example: 3, 5, 7, 11, 13 etc.
  2. Automorphic number: An automorphic number is the number which is contained in the last digit(s) of its square. Example: 25 is an automorphic number as its square is 625 and 25 is present as the last two digits.
import java.util.Scanner;

public class KboatPrimeAutomorphic
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("1. Prime number");
        System.out.println("2. Automorphic 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 c = 0;
            for (int i = 1; i <= num; i++) {
                if (num % i == 0) {
                    c++;
                }
            }
            if (c == 2) 
                System.out.println(num + " is Prime");
            else
                System.out.println(num + " is not Prime");
            break;

            case 2:
            int numCopy = num;
            int sq = num * num;
            int d = 0;

            /*
             * Count the number of 
             * digits in num
             */
            while(num > 0) {
                d++;
                num /= 10;
            }

            /*
             * Extract the last d digits
             * from square of num
             */
            int ld = (int)(sq % Math.pow(10, d));

            if (ld == numCopy)
                System.out.println(numCopy + " is automorphic");
            else
                System.out.println(numCopy + " is not automorphic");
            break;

            default:
            System.out.println("Incorrect Choice");
            break;
        }
    }
}
Output
BlueJ output of KboatPrimeAutomorphic.java
BlueJ output of KboatPrimeAutomorphic.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, Program, 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