- 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 9
Conditional Constructs in Java
Class 9 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Java Programs - Composite Number & Smallest Digit (Menu-Driven)
Using the switch statement, write a menu driven program:
1. 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...
2. 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.
import java.util.Scanner;
public class MenuDrivenNumberCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("----- MENU -----");
System.out.println("1. Check Composite Number");
System.out.println("2. Find Smallest Digit");
System.out.print("Enter your choice (1 or 2): ");
int choice = sc.nextInt();
switch (choice) {
case 1: // Composite Number Check
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++;
break;
}
}
if (count > 0) {
System.out.println(num + " is a Composite Number.");
} else {
System.out.println(num + " is NOT a Composite Number.");
}
break;
case 2: // Smallest Digit
System.out.print("Enter an integer: ");
int n = sc.nextInt();
int smallest = 9;
int digit;
while (n > 0) {
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
Output 1 : (Composite Number)
SAMPLE INPUT : Enter your choice (1 or 2): 1 Enter a number: 9 SAMPLE OUTPUT : 9 is a Composite Number.Output 2 : (Not Composite)
SAMPLE INPUT : Enter your choice (1 or 2): 1 Enter a number: 7 SAMPLE OUTPUT : 7 is NOT a Composite Number.Output 3 : (Smallest Digit)
SAMPLE INPUT : Enter your choice (1 or 2): 2 Enter an integer: 6524 SAMPLE OUTPUT : Smallest digit is 2Output 4 : (Invalid Choice)
SAMPLE INPUT : Enter your choice (1 or 2): 5 SAMPLE OUTPUT : Invalid choice! Please select 1 or 2.
Explanation
1. Menu using switch
- The program allows the user to choose between two operations.
- switch executes the selected case.
2. Composite Number Logic
- A number is composite if it has a factor other than 1 and itself.
- Loop checks divisibility from 2 to num/2.
3. Smallest Digit Logic
- Digits are extracted using % 10.
- Minimum digit is tracked using a variable.
4. Default Case
- Displays an error message for invalid menu choices.
