Java Program to Check Buzz Number

Buzz number in java | A number is called a buzz number if it is divisible by 7 or it ends with 7. For example- 7, 17, 27, 37, 47 are buzz numbers because they end with 7. Similarly, the numbers 7, 14, 21, 28, 35, 49 are also buzz numbers because they are divisible by the number 7.

The first few buzz numbers are:- 7 14 17 21 27 28 35 37 42 47 49 56 57 63 67 70 77 84 87 91 97 98 105 107 112 117 119 126 127 133 137 140 147 154 157 161 167 168 175 177 182 187 189 196 197

Java program to check whether the given number is buzz number or not

import java.util.Scanner;

public class BuzzNumber {

   // method to check buzz number
   public static boolean isBuzz(int number) {

      // buzz numbers are ends with 7
      // or it is divisible by 7
      if( number%10 == 7 || number%7 == 0) 
         return true;

      // else it is not a buzz number
      return false;
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;

      // read the input 
      Scanner scan =  new Scanner(System.in);
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check the number is buzz number or not
      if(isBuzz(number))
         System.out.println(number+" is a"
		    	+ " buzz number");
      else
         System.out.println(number+" is not a"
	      	  	+ " buzz number");

      // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases are:-

Enter an integer number:: 10
10 is not a buzz number

Enter an integer number:: 28
28 is a buzz number

The time complexity of the above program is O(1).

Also see:- Special numberMagic numberArmstrong numberPerfect numberEvil NumberSpy NumberSunny number in Java

Java program to find all buzz number in a given range

Using a loop we can find all buzz numbers which exist in the given range.

import java.util.Scanner;

public class BuzzNumberInRange {

   // method to check buzz number
   public static boolean isBuzz(int number) {

      // buzz numbers are ends with 7
      // or it is divisible by 7
      if( number%10 == 7 || number%7 == 0) 
         return true;

      // else it is not a buzz number
      return false;
   }

   public static void main(String[] args) {

      // declare variables
      int minRange = 0, maxRange = 0;

      // create Scanner class object
      Scanner scan = new Scanner(System.in);

      // read inputs
      System.out.print("Enter min value of range:: ");
      minRange = scan.nextInt();
      System.out.print("Enter max value of range:: ");
      maxRange = scan.nextInt();

      // find all buzz number
      System.out.println("The buzz numbers from "+
		minRange+" to "+ maxRange+" are:: ");

      for(int i=minRange; i<=maxRange; i++) {
         if(isBuzz(i))
         System.out.print(i+" ");
      }

      // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases are:-

Enter min value of range:: 1
Enter max value of range:: 100
The buzz numbers from 1 to 100 are::
7 14 17 21 27 28 35 37 42 47 49 56 57 63 67 70 77 84 87 91 97 98

Enter min value of range:: 100
Enter max value of range:: 200
The buzz numbers from 100 to 200 are::
105 107 112 117 119 126 127 133 137 140 147 154 157 161 167 168 175 177 182 187 189 196 197

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *