In this tutorial, we will start with what is Buzz Number and Write a Java Program to check whether the number is Buzz Number or not.
Buzz Number
A Buzz number is one of the special numbers that end with 7 or is divisible by 7.
For example: 7, 17, 27, 37, etc are buzz numbers because they end with 7, and the numbers 7, 14, 21, 28, etc are also buzzed numbers because they are divisible by 7.
Program Approach to check the Buzz Number.
1. First take a number that needs to be checked for Buzz.
2. We then apply a condition to check the two conditions needed to check for the Buzz number.
- We check if the last digit is 7 or not with
%
operator (number % 10 == 7
), which gives us the remainder. - Then, we check the divisibility of a number by 7 with
/
operator (num / 7 == 0
).
3. Both of these conditions are checked together in with || operator, in which if either one is true then || (or) operator returns a true value. Indicating the number is Buzz number.
4. Else the number is not a Buzz number.
Java Program to check whether the number is Buzz Number or not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Scanner; public class BuzzNumber { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number to check for Buzz: "); int num = sc.nextInt(); //Condition for Buzz number if (num % 10 == 7 || num % 7 == 0) System.out.println(num + " is a Buzz number."); else System.out.println(num + " is a not Buzz number."); } } |
Output:
//Run: 1
Enter a number to check for Buzz: 17
17 is a Buzz number.
//Run: 2
Enter a number to check for Buzz: 32
32 is a not Buzz number.
Java program to display all the Buzz number in a given Range
This is another example where we will print all the Buzz numbers in a given range in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.Scanner; public class BuzzNumber { public static void main(String args[]) { int lRange, uRange; Scanner sc = new Scanner(System.in); System.out.print("Enter the lower range: "); lRange = sc.nextInt(); System.out.print("Enter the Upper range: "); uRange = sc.nextInt(); System.out.print("List of all the buzz number between " + lRange + " and " + uRange); for (int i = lRange; i <= uRange; i++) { //Condition for Buzz number if (i % 10 == 7 || i % 7 == 0) System.out.println(i); } } } |
Output:
Enter the lower range: 0
Enter the Upper range: 50
List of all the buzz number between 0 and 50
7
14
17
21
27
28
35
37
42
47
49
You may create a separate boolean function to check for the Buzz number and return the number to the main function. Call the boolean function inside the for loop in the main function.