Check Whether a Number Is a Buzz Number in Java Without Using Modulo Operator
A Buzz Number is a number that is either:
- Divisible by 7, or
- Ends with the digit 7
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
- 7 → Buzz Number (divisible by 7 and ends with 7)
- 14 → Buzz Number (divisible by 7)
- 27 → Buzz Number (ends with 7)
- 25 → Not a Buzz Number
How Can We Avoid the Modulo Operator?
Instead of using number % 7, we can:
- Use integer division to remove multiples of 7
- Check the last digit using subtraction and division
Logic Explanation
- Store the original number.
- Check if the number ends with 7 using arithmetic.
- Check divisibility by 7 by subtracting multiples of 7.
- 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
- No modulo (%) operator is used.
- Uses basic arithmetic operations only.
- Works for positive integers.
Practice Challenges
- Check Buzz Number using only addition and subtraction.
- Modify the program to handle negative numbers.
- 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.