Buzz Number in Java

A Buzz number is a special type of number that has one of the following characteristics:

  1. The number is divisible by 7: If a number can be evenly divided by 7 without leaving any remainder, it is considered a Buzz number.
  2. The number ends with the digit 7: If the last digit of a number is 7, it is also considered a Buzz number.

For example, let's consider the number 27. It is not divisible by 7, so it does not satisfy the first condition. However, it ends with the digit 7, so it meets the second condition. Therefore, 27 is a Buzz number.

Example 1

In the above code, the isBuzzNumber() method takes an integer as input and returns true if the number is a Buzz Number, and false otherwise. It checks if the number is divisible by 7 (number % 7 == 0) or if it contains the digit 7 (String.valueOf(number).contains("7")).

FileName: BuzzNumberChecker.java

public class BuzzNumberChecker {

    public static void main(String[] args) {

        int number = 47; // Example number to check

        if (isBuzzNumber(number)) {

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

        } else {

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

        }

    }

    public static boolean isBuzzNumber(int number) {

        // Check if the number is divisible by 7 or contains the digit 7

        return number % 7 == 0 || String.valueOf(number).contains("7");

    }

}

Output

47 is a Buzz Number.

Example 2

In the above program, The isBuzzNumber () method takes an integer number as input and returns true if the number is a Buzz number (ends with 7 or is divisible by 7), and false otherwise.

In the main() method, it prompts the user to enter two numbers (n1 and n2) using the Scanner class. It then calls the isBuzzNumber () method for each input number and displays whether each number is a Buzz number or not.

FileName: BuzzNumberExample.java

import java.util.Scanner;

class BuzzNumberExample {

    // Method to check if a number is a Buzz number

    static boolean isBuzzNumber(int number) {

        if (number % 10 == 7 || number % 7 == 0)

            return true; // Returns true if number is Buzz

        else

            return false; // Returns false if number is not Buzz

    }

    public static void main(String args[]) {

        int n1, n2;

        Scanner sc = new Scanner(System.in);

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

        n1 = sc.nextInt(); // Read first number from user

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

        n2 = sc.nextInt(); // Read second number from user

        if (isBuzzNumber(n1))

            System.out.println(n1 + " is a Buzz number"); // Print if n1 is a Buzz number

        else

            System.out.println(n1 + " is not a Buzz number"); // Print if n1 is not a Buzz number

        if (isBuzzNumber(n2))

            System.out.println(n2 + " is a Buzz number"); // Print if n2 is a Buzz number

        else

            System.out.println(n2 + " is not a Buzz number"); // Print if n2 is not a Buzz number

    }

}

Output

Enter first number:

27

Enter second number:

11

27 is a Buzz number

11 is not a Buzz number

Example 3

In this code, the main method prompts the user to enter the value of the range. It then iterates from 1 to the specified range using a for loop. For each number, it calls the isBuzzNumber method to determine if it is a Buzz number. If a number satisfies the Buzz number condition, it is printed.

The isBuzzNumber method checks whether a number is a Buzz number by verifying if it ends with 7 or is divisible by 7. If a number meets either of these conditions, it is considered a Buzz number, and it is printed.

FileName: FindAllBuzzNumbers.java

import java.util.Scanner;

class FindAllBuzzNumbers {

    public static void main(String args[]) {

        int range;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the value of range");

        range = sc.nextInt(); // Read the range value from user

        for (int i = 1; i <= range; i++) {

            if (isBuzzNumber(i)) {

                System.out.println(i + " is a Buzz number"); // Print if i is a Buzz number

            }

        }

    }

    // Method to check if a number is a Buzz number

    static boolean isBuzzNumber(int number) {

        return number % 10 == 7 || number % 7 == 0;

    }

}

Output

Enter the value of range

70

7 is a Buzz number

14 is a Buzz number

17 is a Buzz number

21 is a Buzz number

27 is a Buzz number

28 is a Buzz number

35 is a Buzz number

37 is a Buzz number

42 is a Buzz number

47 is a Buzz number

49 is a Buzz number

56 is a Buzz number

57 is a Buzz number

63 is a Buzz number

67 is a Buzz number

70 is a Buzz number

← Prev Next →