Practice: FizzBuzz & Number Problems
Practice: FizzBuzz & Number Problems
You have now learned all the control-flow and loop tools Java offers. This final lesson puts everything together through three classic problems you will encounter in technical interviews and real code reviews: FizzBuzz, prime-number detection, and factorial calculation. Working through them trains the habit of translating plain-English requirements into clean, correct Java.
Problem 1 — FizzBuzz
Task: Print the numbers from 1 to 100. For multiples of 3 print Fizz, for multiples of 5 print Buzz, and for multiples of both 3 and 5 print FizzBuzz.
The key insight is order of conditions. The combined check (% 3 == 0 && % 5 == 0) must come first, otherwise it will never be reached:
% 3 == 0 check before the combined check, the number 15 prints Fizz and never reaches FizzBuzz. Always test the most specific condition first.
A clean alternative builds the output string before printing, which avoids repeating System.out.println:
if instead of rewriting every branch. Writing code that is easy to extend is a sign of experience.
Problem 2 — Prime Number Detection
A prime number is greater than 1 and has no divisors other than 1 and itself. The brute-force approach checks every number from 2 up to n - 1, but we only need to check up to Math.sqrt(n): if n has a factor larger than its square root, the matching smaller factor has already been found.
i * i <= n instead of i <= Math.sqrt(n)? Both are correct. The multiplication form avoids a floating-point square root call on every iteration, keeping the loop slightly faster. For the numbers you will deal with as a beginner the difference is invisible, but it is the idiomatic choice in Java.
Problem 3 — Factorial
The factorial of a non-negative integer n (written n!) is the product of all positive integers from 1 to n. By definition, 0! = 1.
Iterative version — straightforward and efficient:
Notice the return type is long rather than int13! (6,227,020,800) overflows a 32-bit int, so using long gives you safe results up to 20!.
factorial(n) = n * factorial(n - 1). The iterative form above is preferred in production because it does not risk a stack overflow for large inputs and is easier to reason about.
Putting It All Together
All three problems follow the same pattern you have practiced throughout this tutorial:
- Read the rule in plain English.
- Choose the right loop (
forfor a known range,whilewhen the exit condition is dynamic). - Inside the loop, write
if/else if/elseconditions in order from most specific to least specific. - Use
breakor earlyreturnto stop as soon as you have an answer — do not keep looping unnecessarily.
Summary
You have now completed the Control Flow & Loops tutorial. FizzBuzz taught you correct condition ordering and string-building patterns. Prime detection showed how a single mathematical observation (square-root bound) turns a slow loop into a fast one. Factorial demonstrated safe integer sizing and the value of iterative over recursive solutions for simple accumulations. These three problems are a strong foundation for every algorithm challenge ahead.