Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7946677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:13:12+00:00 2026-06-04T01:13:12+00:00

For those who don’t know, FizzBuzz is the following problem: Write a program that

  • 0

For those who don’t know, FizzBuzz is the following problem:

Write a program that prints the numbers from 1 to 100. But for
multiples of three print "Fizz" instead of the number and for the
multiples of five print "Buzz". For numbers which are multiples of
both three and five print "FizzBuzz".

Every FizzBuzz solution I find is either some crazy esoteric solution made for the sake of being original, or your basic if-else chain:

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

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

I am looking for a simple solution that aims to take out the "FizzBuzz" if statement. I have this in mind:

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

    if (i % 3 == 0) 
       System.out.print("Fizz");
    if (i % 5 == 0) 
       System.out.println("Buzz")
    else
       System.out.println(i);
}

But this doesn’t work. I assume it would be able to print FizzBuzz by entering both ifs, for Fizz and for Buzz, but if the number is, for example, 3, it would print Fizz3. How do I avoid this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:13:14+00:00Added an answer on June 4, 2026 at 1:13 am

    What you’re trying to do is

    if (a)
        ...
    if (b)
        ...
    else // if neigther a nor b
        ...
    

    This is simply not possible. An else can only belong to a single if. You have to go with the slightly longer variant.

    To avoid doing redundant evaluations of the modulo operator, you could formulate the loop body as

    boolean fizz = i % 3 == 0;
    boolean buzz = i % 5 == 0;
    
    if (fizz) 
       System.out.print("Fizz");
    if (buzz)
       System.out.print("Buzz");
    if (!(fizz || buzz))
       System.out.print(i);
    
    System.out.println();
    

    Another one would be

    String result = "";
    
    if (i % 3 == 0)   result = "Fizz";
    if (i % 5 == 0)   result += "Buzz";
    if (result == "") result += i;
    
    System.out.println(result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For all those who don't know what I am talking about, it's glTail from
For those who don't know the model. You can read this pdf . I
For those who don't know GNASH is an open source flash movie player. It
I'm learning processing (for those who don't know its a java-based language geared towards
I have two Resources files under App_GlobalResources MyApp.resx MyApp.sv.resx for those who don't know:
Quick question: For those who don't know CakePHP Model->count(); always returns an integer. If
I have a website that I host with PHPFog. PHPFog, for those who don't
For those who don't know what a bookmarklet is: http://en.wikipedia.org/wiki/Bookmarklet
Is there a reliable equivalent of xkill for Windows? For those who don't know
For those who still don't know about Responsive Design I suggest this link As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.