- 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
Conditional Constructs in Java
Chapter 8
Conditional Constructs in Java
Class 10 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Java Program: Menu Driven Program - Composite Number & Smallest Digit
41. Using the switch statement, write a menu driven program:
- To check and display whether a number input by the user is a composite number or not.
A number is said to be composite, if it has one or more than one factors excluding 1 and the number itself.
Example: 4, 6, 8, 9... - To find the smallest digit of an integer that is input:
Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed.
📋 Menu Options
- Check whether a number is Composite
- Find the Smallest Digit of a number
import java.util.Scanner;
public class MenuCompositeSmallestDigit {
public static void main(String[] args) {
// Title
System.out.println("MENU DRIVEN PROGRAM");
System.out.println("-------------------");
System.out.println("1. Check Composite Number");
System.out.println("2. Find Smallest Digit");
Scanner sc = new Scanner(System.in);
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter a number: ");
int num = sc.nextInt();
int count = 0;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0)
count++;
}
if (count > 0)
System.out.println(num + " is a Composite Number");
else
System.out.println(num + " is NOT a Composite Number");
break;
case 2:
System.out.print("Enter an integer: ");
int n = sc.nextInt();
int smallest = 9;
while (n > 0) {
int digit = n % 10;
if (digit < smallest)
smallest = digit;
n = n / 10;
}
System.out.println("Smallest digit is " + smallest);
break;
default:
System.out.println("Invalid choice! Please select 1 or 2.");
}
sc.close();
}
}
Output
Sample Run 1 Enter your choice: 1 Enter a number: 6 6 is a Composite Number Sample Run 2 Enter your choice: 2 Enter an integer: 6524 Smallest digit is 2 Invalid Choice Enter your choice: 5 Invalid choice! Please select 1 or 2.
📝 Explanation
🔹 Composite Number Check
- Counts factors excluding 1 and the number
- If factor count > 0 → Composite
🔹 Smallest Digit
- Extracts digits using % 10
- Compares each digit to find the smallest
🔹 switch-case
- Executes logic based on user choice
- Displays error message for invalid input
