Java Computer Eclipse Program: Recall that you can tell if a number is divisible
ID: 3662479 • Letter: J
Question
Java Computer Eclipse Program:
Recall that you can tell if a number is divisible by another by using the modulus operator (%) which gives you the remainder from division. If the result is zero (there is no remainder) then the first number is divisible by the second. For your number game, we will count from 1 to 30 using the following rules: 1. If a number is divisible by 5, then print "beep" instead of the number 2. If a number is divisible by 8, then print "buzz" instead of the number 3. If a number is divisible by 12, then print "zap" instead of the number Remember: if the number is divisible by both numbers, both words should print on the same line.
Explanation / Answer
/**
* @author Srinivas Palli
*
*/
public class NumberGame {
/**
* @param args
*/
public static void main(String[] args) {
for (int i = 1; i <= 30; i++) {
System.out.print(i + ". ");
// 1. If a number is divisible by 5, then print "beep" instead of
// the number
if (i % 5 == 0)
System.out.print(" beep ");
// 2. If a number is divisible by 8, then print "buzz" instead of
// the number
if (i % 8 == 0)
System.out.print(" buzz ");
// 3. If a number is divisible by 12, then print "zap" instead of
// the number
if (i % 12 == 0)
System.out.print(" zap ");
System.out.println();
}
}
}
OUTPUT:
1.
2.
3.
4.
5. beep
6.
7.
8. buzz
9.
10. beep
11.
12. zap
13.
14.
15. beep
16. buzz
17.
18.
19.
20. beep
21.
22.
23.
24. buzz zap
25. beep
26.
27.
28.
29.
30. beep
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.