Buzz Number in Java

In the realm of Java, there exists a distinct type of number known as a “Buzz number.” These numbers possess the characteristic of either ending with the digit 7 or being divisible by 7. While Buzz numbers may not receive as much attention as Prime or Armstrong numbers, they can still be encountered in interviews and programming discussions.

In simpler terms, a number can be classified as a Buzz number if it satisfies either of the following conditions: it ends with the digit 7, or it is divisible by 7.

Let’s consider a few examples of Buzz numbers.

  1. The number 42 is a Buzz number since it is divisible by 7.
  2. Similarly, the number 107 is a Buzz number because it ends with the digit 7.
  3. The number 147 qualifies as a Buzz number because it satisfies both criteria: it ends with the digit 7 and is divisible by 7.
  4. On the other hand, the number 134 is not a Buzz number since it does not meet either condition. It neither ends with the digit 7 nor is divisible by 7.

To determine if a given number is a Buzz number, we can follow the following steps:

  1. Obtain the input number.
  2. Extract the last digit of the number and check if it is equal to 7. If it is, then we can conclude that “the number is a Buzz number.”
  3. Calculate the remainder of the number when divided by 7. If the remainder is equal to 0, we can conclude that “the number is a Buzz number.”
  4. If none of the above conditions are met, we can conclude that “the number is not a Buzz number.”

BuzzNumberExample.java

//import required classes and packages  
import Java.util.*;   
import java.io.*;   
import java.util.Scanner;  
  
//create BuzzNumberExample class to check whether the given number is Buzz number or not  
class BuzzNumberExample {   
    
    // create checkNumber() method that returns true when it founds number Buzz   
    static boolean checkNumber(int number)   
    {   
        // check whether the number ends with 7, is divisible by 7 or not  
        if(number % 10 == 7 || number % 7 == 0)  
            return true;    //returns true when the number is Buzz  
        else  
            return false;   //returns flase when the number is not Buzz  
    }   
    
    // main() method start  
    public static void main(String args[])   
    {     
        int n1, n2;  
          
        //create scanner class object to get input from user  
        Scanner sc=new Scanner(System.in);  
          
        //show custom message  
        System.out.println("Enter first number");  
          
        //store user entered value into variable n1  
        n1 = sc.nextInt();  
          
        //show custom message  
        System.out.println("Enter second number");  
          
        //store user entered value into variable n2  
        n2 = sc.nextInt();  
          
        if (checkNumber(n1))   
            System.out.println(n1 + " is a Buzz number");   
        else  
            System.out.println(n1 + " is not a Buzz number");   
        if (checkNumber(n2))   
            System.out.println(n2 + " is a Buzz number");   
        else  
            System.out.println(n2 + " is not a Buzz number");   
    }   
}

FindAllBuzzNumbers.java

import java.util.*;   
import java.io.*;   
import java.util.Scanner;  
  
//create FindAllBuzzNumber class to get all the Buzz number in a given range  
class FindAllBuzzNumbers  
{  
    //main() method start  
    public static void main(String args[])  
    {  
        int range;  
          
        //create scanner class object  
        Scanner sc=new Scanner(System.in);  
          
        //show custom message  
        System.out.println("Enter the value of range");  
          
        //store user entered value into variable range  
        range = sc.nextInt();  
  
        for(int i = 1; i <= range; i++)  
            checkNumber(i);  
    }  
  
     // create checkNumber() method to check Buzz number  
    static void checkNumber(int number)   
    {   
        // check whether the number ends with 7, is divisible by 7 or not  
        if(number % 10 == 7 || number % 7 == 0)  
            System.out.println(number + " is an Buzz number");  
    }  
  
}

Output


Ensure regular visits to tutorials.freshersnow.com to expand your knowledge on subjects such as Buzz Number in Java.