Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
buzz
communitycreator

How to check Buzz numbers in Java

Harsh Jain

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;  
    else  
      return 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");   
    else  
      System.out.println(n + " is not a Buzz number");   
    }   
}   

Enter the input below

Check whether the number is a Buzz number or not

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 int data 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 int data type.

  • In line 16, we make an object of the Scanner class to use the methods available in it.

  • In line 17, we read the input by the user and store it in a variable of int data 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 number is a Buzz number, otherwise print the number is not a Buzz number.

In this way, we can check whether a number is a Buzz number or not.

RELATED TAGS

java
buzz
communitycreator
RELATED COURSES

View all Courses

Keep Exploring