Buzz Number Java

Buzz number is another special number in Java that ends with digit 7 or divisible by 7. Unlike Prime and Armstrong numbers, the Buzz number is not so popular and asked by the interviewers.

In simple words, a number is said to be Buzz if it ends with 7 or is divisible by 7.

Let’s take some examples of Buzz numbers.

  1. 42 is a Buzz number because it is divisible by 7.
  2. 107 is a Buzz number because it ends with 7.
  3. 147 is a Buzz number because it ends with 7 and also divisible by 7.
  4. 134 is not a Buzz number because it is neither end with 7 nor divisible by 7.

These are the following steps that we use to check whether the given number is a Buzz number or not.

  1. We first take a number.
  2. We then find the last digit of the number and check whether it is equals to 7 or not. If it equals 7, print “the number is a Buzz” number.
  3. We then find the remainder of the number with 7. If the remainder equal to 0, print “the number is a Buzz number”.
  4. Else print “number is not a buzz number”.

Let’s implement a program to check whether the user entered number is a Buzz number or not.

BuzzNumberExample.java

  1. //import required classes and packages  
  2. import Java.util.*;   
  3. import java.io.*;   
  4. import java.util.Scanner;  
  5.   
  6. //create BuzzNumberExample class to check whether the given number is Buzz number or not  
  7. class BuzzNumberExample {   
  8.     
  9.     // create checkNumber() method that returns true when it founds number Buzz   
  10.     static boolean checkNumber(int number)   
  11.     {   
  12.         // check whether the number ends with 7, is divisible by 7 or not  
  13.         if(number % 10 == 7 || number % 7 == 0)  
  14.             return true;    //returns true when the number is Buzz  
  15.         else  
  16.             return false;   //returns flase when the number is not Buzz  
  17.     }   
  18.     
  19.     // main() method start  
  20.     public static void main(String args[])   
  21.     {     
  22.         int n1, n2;  
  23.           
  24.         //create scanner class object to get input from user  
  25.         Scanner sc=new Scanner(System.in);  
  26.           
  27.         //show custom message  
  28.         System.out.println(“Enter first number”);  
  29.           
  30.         //store user entered value into variable n1  
  31.         n1 = sc.nextInt();  
  32.           
  33.         //show custom message  
  34.         System.out.println(“Enter second number”);  
  35.           
  36.         //store user entered value into variable n2  
  37.         n2 = sc.nextInt();  
  38.           
  39.         if (checkNumber(n1))   
  40.             System.out.println(n1 + ” is a Buzz number”);   
  41.         else  
  42.             System.out.println(n1 + ” is not a Buzz number”);   
  43.         if (checkNumber(n2))   
  44.             System.out.println(n2 + ” is a Buzz number”);   
  45.         else  
  46.             System.out.println(n2 + ” is not a Buzz number”);   
  47.     }   
  48. }   

Output

Buzz Number Java

Let’s implement one more program to get all the Buzz number in a given range

FindAllBuzzNumbers.java

  1. //import required classes and packages  
  2. import java.util.*;   
  3. import java.io.*;   
  4. import java.util.Scanner;  
  5.   
  6. //create FindAllBuzzNumber class to get all the Buzz number in a given range  
  7. class FindAllBuzzNumbers  
  8. {  
  9.     //main() method start  
  10.     public static void main(String args[])  
  11.     {  
  12.         int range;  
  13.           
  14.         //create scanner class object  
  15.         Scanner sc=new Scanner(System.in);  
  16.           
  17.         //show custom message  
  18.         System.out.println(“Enter the value of range”);  
  19.           
  20.         //store user entered value into variable range  
  21.         range = sc.nextInt();  
  22.   
  23.         for(int i = 1; i <= range; i++)  
  24.             checkNumber(i);  
  25.     }  
  26.   
  27.      // create checkNumber() method to check Buzz number  
  28.     static void checkNumber(int number)   
  29.     {   
  30.         // check whether the number ends with 7, is divisible by 7 or not  
  31.         if(number % 10 == 7 || number % 7 == 0)  
  32.             System.out.println(number + ” is an Buzz number”);  
  33.     }  
  34.   
  35. }  

Output

Buzz Number Java

Leave a Comment