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

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 - Buzz Number and GCD Checker


27. Write a menu driven program to accept a number from the user and check whether it is a BUZZ number or to accept any two numbers and to print the GCD of them.

(1). A BUZZ number is the number which either ends with 7 or is divisible by 7.

(2). GCD (Greatest Common Divisor) of two integers is calculated by continued division method. Divide the larger number by the smaller; the remainder then divides the previous divisor. The process is repeated till the remainder is zero. The divisor then results the GCD.

Program Title: Buzz Number and GCD Checker

import java.util.Scanner;

 

public class BuzzGCDMenu {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        System.out.println("MENU");

        System.out.println("1. Check Buzz Number");

        System.out.println("2. Find GCD of Two Numbers");

        System.out.print("Enter your choice: ");

        int choice = sc.nextInt();

 

        switch (choice) {

            case 1:

                // Check Buzz number

                System.out.print("Enter a number: ");

                int num = sc.nextInt();

 

                if (num % 10 == 7 || num % 7 == 0) {

                    System.out.println(num + " is a Buzz Number.");

                } else {

                    System.out.println(num + " is NOT a Buzz Number.");

                }

                break;

 

            case 2:

                // Compute GCD

                System.out.print("Enter first number: ");

                int a = sc.nextInt();

                System.out.print("Enter second number: ");

                int b = sc.nextInt();

 

                int x = a, y = b;

 

                while (y != 0) {

                    int remainder = x % y;

                    x = y;

                    y = remainder;

                }

 

                System.out.println("GCD of " + a + " and " + b + " is: " + x);

                break;

 

            default:

                System.out.println("Invalid choice! Please select 1 or 2.");

        }

 

        sc.close();

    }

}

Output

Sample Output 1 (Buzz Number)
MENU
1. Check Buzz Number
2. Find GCD of Two Numbers
Enter your choice: 1
Enter a number: 35
35 is a Buzz Number.

Sample Output 2 (GCD)
MENU
1. Check Buzz Number
2. Find GCD of Two Numbers
Enter your choice: 2
Enter first number: 48
Enter second number: 180
GCD of 48 and 180 is: 12

📝 Explanation

1. Buzz Number:

  • A number is a Buzz number if it ends with 7 (num % 10 == 7)
    OR is divisible by 7 (num % 7 == 0).

2. GCD (Euclidean Algorithm):

  • Continuously divide the larger number by the smaller number.
  • Replace the larger number with the smaller, and the smaller with the remainder.
  • Repeat until remainder = 0.
  • The divisor at that point is the GCD.

3. Switch-case is used for menu selection.