ICSE Class 9 Computer Applications Question 12 of 17

Conditional Constructs in Java — Question 12

Back to all questions
12
Question

Question 12

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.

Answer
import java.util.Scanner;

public class KboatBuzzAutomorphic
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("1. Buzz number");
        System.out.println("2. Automorphic number");
        System.out.print("Enter your choice: ");
        int choice = in.nextInt();
        System.out.print("Enter number: ");
        int num = in.nextInt();

        switch (choice) {
            case 1:
            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:
            int sq = num * num;
            int d = 0;
            int t = num;

            /*
             * Count the number of 
             * digits in num
             */
            while(t > 0) {
                d++;
                t /= 10;
            }

            /*
             * Extract the last d digits
             * from square of num
             */
            int ld = (int)(sq % Math.pow(10, d));

            if (ld == num)
                System.out.println(num + " is automorphic");
            else
                System.out.println(num + " is not automorphic");
            break;

            default:
            System.out.println("Incorrect Choice");
            break;
        }
    }
}
Output
BlueJ output of KboatBuzzAutomorphic.java
BlueJ output of KboatBuzzAutomorphic.java