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 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 2

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