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