How to check Buzz numbers in Java
In this shot, we will learn how to check whether or not a number is a Buzz number.
Buzz numbers are those numbers that are divisible by seven or end with seven. For example, 57 is a buzz number because the number ends with seven. Another example is 28 because the number is divisible by seven.
Code
Let’s look at the code to check whether a given number is a Buzz number or not.
import java.util.Scanner;class Main {static boolean checkBuzz(int num){if(num % 10 == 7 || num % 7 == 0)return true;elsereturn false;}public static void main(String args[]){int n;Scanner sc=new Scanner(System.in);n = sc.nextInt();if (checkBuzz(n))System.out.println(n + " is a Buzz number");elseSystem.out.println(n + " is not a Buzz number");}}
Enter the input below
Explanation
-
From lines 1 to 2, we imported the required packages.
-
In line 3, we made a class
Main. -
From lines 5 to 11, we created a unary function with a parameter of
intdata type that checks whether the number passed as an argument to the function is a Buzz number or not.- The function checks if the number is divisible by seven by dividing it by seven and getting a remainder of zero.
- The function also checks by dividing the number by ten, and if it gives a remainder of seven, the number ends with seven and is, therefore, a Buzz number.
-
In line 15, we declared a variable of
intdata type. -
In line 16, we make an object of the
Scannerclass to use the methods available in it. -
In line 17, we read the input by the user and store it in a variable of
intdata type. -
From lines 18 to 21, we call the
checkBuzz()function with the required argument, which is the input we read from the user. If it returns true then, we print the message that the numberis a Buzz number, otherwise print the numberis not a Buzz number.
In this way, we can check whether a number is a Buzz number or not.