FizzBuzz question

Posted on 2013-01-11
Last Modified: 2013-01-11
"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”." from

the below prints the multiples of 3 and of 5 but not of both. Why?
#include <iostream>

using namespace std;
int main(int argc, const char * argv[])
    for (int i = 1; i < 101; i++)
        if(i % 3 == 0)
            cout << "Fizz" << endl;
        else if(i % 5 == 0)
            cout << "Buzz!" << endl;
        else if ((i % 3 == 0) && (i % 5 == 0))
            cout << "FizzBuzz, WHAAAAT!" << endl;
        cout << i << endl;
    return 0;

Open in new window

Question by:Mark_Co
  • 2
LVL 86

Assisted Solution

jkr earned 167 total points
ID: 38769358

else if ((i % 3 == 0) && (i % 5 == 0))

Open in new window

which should read
else if ((i % 3 == 0) || (i % 5 == 0))

Open in new window

('or') instead.
LVL 31

Assisted Solution

farzanj earned 333 total points
ID: 38769371
You statement:
else if ((i % 3 == 0) && (i % 5 == 0))

Is correct.  However this condition will never reach and if a number is divisible by both 3 and 5, 3 being in the first condition will win.

There are many ways to fix it but one way is to change the conditions like

First condition
if ((i % 3 == 0) && (i % 5 != 0))

And second like
if ((i % 3 != 0) && (i % 5 == 0))
LVL 31

Accepted Solution

farzanj earned 333 total points
ID: 38769379
At present suppose i = 15

i %3 ==0 is true
i % 5 == 0 is also true

But the first condition wins.

If you reverse the order of your conditions, put the third condition first, it should work too!

Author Closing Comment

ID: 38769430
Yes, moving the double condition to the top of the if-else tree did the trick. Thanks

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question