Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

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:

  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.

📋 Menu Options

  1. Check whether a number is Composite
  2. 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