I'm a first year college student, Computer Science major. This is my crack at the FizzBuzz interview question in Java. What kind of improvements could I make?
//Prints numbers 1 - 100, 25 values per line.
//Numbers that are multiples of 3 are replaced with Fizz
//Numbers that are mulitples of 5 are replaced with Buzz
//Numbers that are multiples of both 3 and 5 are replaced with FizzBuzz
public class FizzBuzz {
public static void main(String[] args) {
String fizz = "Fizz";
String buzz = "Buzz";
String fizzBuzz = "FizzBuzz";
//Loop from 1 through 100
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0 && i % 5 == 0) { //Checks for numbers that are both multiples of 5 and 3
if (i % 25 != 0) { //Control flow allows 25 values printer per line
System.out.printf("%4.8s ", fizzBuzz);
}
else {
System.out.printf("%4.8s\n ", fizzBuzz);
System.out.println();
}
}
else if (i % 3 == 0) { //Checks for numbers that are multiples of 3
if (i % 25 != 0) { //Control flow allows 25 values printer per line
System.out.printf("%4.8s ", fizz);
}
else {
System.out.printf("%4.8s\n ", fizz);
System.out.println();
}
}
else if (i % 5 == 0) { //Checks for numbers that are multiples of 5
if (i % 25 != 0) { //Control flow allows 25 values printer per line
System.out.printf("%4.8s ", buzz);
}
else {
System.out.printf("%4.8s\n ", buzz);
System.out.println();
}
}
else { //Prints numbers that are not multiples of 3 or 5
if (i % 25 != 0) { //Control flow allows 25 values printer per line
System.out.print(i + " ");
}
else {
System.out.println(i + " ");
System.out.println();
}
}
}
}
}