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: Check Prime or Automorphic Number


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).

(i). 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.

(ii). 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.

Program Title: Check Prime or Automorphic Number

import java.util.Scanner;

 

public class PrimeAutomorphic {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        System.out.println("MENU");

        System.out.println("1. Check Prime Number");

        System.out.println("2. Check Automorphic Number");

        System.out.print("Enter your choice: ");

        int choice = sc.nextInt();

 

        System.out.print("Enter a number: ");

        int num = sc.nextInt();

 

        switch (choice) {

            case 1:

                // Check Prime

                if (num <= 1) {

                    System.out.println(num + " is NOT a Prime Number.");

                    break;

                }

                boolean isPrime = true;

                for (int i = 2; i <= Math.sqrt(num); i++) {

                    if (num % i == 0) {

                        isPrime = false;

                        break;

                    }

                }

                if (isPrime) {

                    System.out.println(num + " is a Prime Number.");

                } else {

                    System.out.println(num + " is NOT a Prime Number.");

                }

                break;

 

            case 2:

                // Check Automorphic

                int square = num * num;

                int temp = num;

                boolean isAutomorphic = (square % (int) Math.pow(10, String.valueOf(num).length()) == num);

 

                if (isAutomorphic) {

                    System.out.println(num + " is an Automorphic Number.");

                } else {

                    System.out.println(num + " is NOT an Automorphic Number.");

                }

                break;

 

            default:

                System.out.println("Invalid choice! Please select 1 or 2.");

        }

 

        sc.close();

    }

}

Output

Sample Output 1 (Prime Number)
MENU
1. Check Prime Number
2. Check Automorphic Number
Enter your choice: 1
Enter a number: 17
17 is a Prime Number.

Sample Output 2 (Automorphic Number) 
MENU
1. Check Prime Number
2. Check Automorphic Number
Enter your choice: 2
Enter a number: 25
25 is an Automorphic Number.

📝 Explanation

  1. Prime Number Check:
    • Numbers ≤ 1 are not prime.
    • Check divisibility up to √num for efficiency.
  2. Automorphic Number Check:
    • A number is automorphic if its square ends with the number itself.
    • Use modulus with 10^number_of_digits to check.
  3. Switch-Case handles menu selection.
  4. Default handles invalid choices.