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 - Buzz Number & Automorphic Number (Menu-Driven)


Write a menu driven program to accept a number from the user and check whether it is a Buzz number or an Automorphic number.

i. Automorphic number is a number, whose square's last digit(s) are equal to that number. For example, 25 is an automorphic number, as its square is 625 and 25 is present as the last two digits.

ii. Buzz number is a number, that ends with 7 or is divisible by 7.


import java.util.Scanner;

 

public class NumberCheckMenu {

    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. Check Automorphic Number");

 

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

        int choice = sc.nextInt();

 

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

        int num = sc.nextInt();

 

        switch (choice) {

 

            case 1: // Buzz Number

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

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

                } else {

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

                }

                break;

 

            case 2: // Automorphic Number

                int square = num * num;

                String numStr = String.valueOf(num);

                String squareStr = String.valueOf(square);

 

                if (squareStr.endsWith(numStr)) {

                    System.out.println(num + " is an Automorphic Number.");

                } else {

                    System.out.println(num + " is NOT an Automorphic Number.");

                }

                break;

 

            default:

                System.out.println("Invalid choice!");

        }

 

        sc.close();

    }

}

Output

Output 1 : (Buzz Number)

SAMPLE INPUT : Enter your choice (1 or 2): 1 Enter a number: 49 SAMPLE OUTPUT : 49 is a Buzz Number.

Output 2 : (Buzz Number)

SAMPLE INPUT : Enter your choice (1 or 2): 1 Enter a number: 23 SAMPLE OUTPUT : 23 is NOT a Buzz Number.

Output 3 : (Automorphic Number)

SAMPLE INPUT : Enter your choice (1 or 2): 2 Enter a number: 25 SAMPLE OUTPUT : 25 is an Automorphic Number.

Output 4 : (Automorphic Number)

SAMPLE INPUT : Enter your choice (1 or 2): 2 Enter a number: 12 SAMPLE OUTPUT : 12 is NOT an Automorphic Number.

Explanation

1. Menu Display

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

        System.out.println("2. Check Automorphic Number");

  • Allows the user to choose the operation.

2. Buzz Number Logic

               num % 7 == 0 || num % 10 == 7

  • Divisible by 7 OR
  • Last digit is 7

3. Automorphic Number Logic

  • Square the number.
  • Convert number and square to strings.
  • Check if square ends with the original number.

squareStr.endsWith(numStr)

4. switch Statement

  • Executes the selected option.
  • Handles invalid menu choice.