Skip to content

Fizzbuzz in Java

public class FizzBuzz {
	public static void main(String[] args) {
		boolean flag;
		for (int i = 1; i<=100;i++)
		{
			flag = false;
			if (i%3==0)
			{	
				System.out.print("Fizz");
				flag = true;
			}
			if (i%5==0)
			{
				System.out.print("Buzz");
				flag = true;
			}
			if (flag == false)
			{
				System.out.print(i);
			}
			System.out.println();
		}
	}

}
2 Comments
  1. Anon permalink

    public class FizzBuzz {

    public static void main(String[] args) {

    for(int i=1;i<=100;i++) {

    if(i%15 == 0) {
    System.out.println("FizzBuzz");
    continue;
    }
    if(i%3 == 0) {
    System.out.println("Fizz");
    continue;
    }
    if(i%5==0) {
    System.out.println("Buzz");
    continue;
    }
    System.out.println(i);
    }

    }
    }

    Probably easier than using booleans

    • That’s a perfectly reasonable method as well. I feel using a third conditional to write what the first two can do themselves is awkward, but that’s purely a personal preference.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: