Check Whether a Number Is a Buzz Number in Java Without Using Modulo Operator

Learn how to check whether a number is a Buzz Number in Java without using the modulo (%) operator, using simple arithmetic logic and clean programming concepts.

Check Whether a Number Is a Buzz Number in Java Without Using Modulo Operator

A Buzz Number is a number that is either:

In this program, we will check a Buzz Number without using the modulo (%) operator, which makes the logic more interesting and interview-oriented.

Examples of Buzz Numbers

How Can We Avoid the Modulo Operator?

Instead of using number % 7, we can:

Logic Explanation

  1. Store the original number.
  2. Check if the number ends with 7 using arithmetic.
  3. Check divisibility by 7 by subtracting multiples of 7.
  4. If either condition is true, it is a Buzz Number.

Java Program (Without Using % Operator)


import java.util.Scanner;

public class BuzzNumberWithoutModulo {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        int original = num;

        // Check if number ends with 7
        int lastDigit = num - (num / 10) * 10;

        boolean endsWithSeven = (lastDigit == 7);

        // Check divisibility by 7 without modulo
        int temp = num;
        while (temp > 0) {
            temp = temp - 7;
        }

        boolean divisibleBySeven = (temp == 0);

        if (endsWithSeven || divisibleBySeven) {
            System.out.println(original + " is a Buzz Number");
        } else {
            System.out.println(original + " is NOT a Buzz Number");
        }
    }
}

Sample Output


Enter a number: 27
27 is a Buzz Number

Enter a number: 25
25 is NOT a Buzz Number

Important Notes

Practice Challenges

  1. Check Buzz Number using only addition and subtraction.
  2. Modify the program to handle negative numbers.
  3. Print all Buzz Numbers between 1 and 100 without using %.

This problem strengthens mathematical thinking and shows how operators like modulo can be replaced using pure logic.