Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

FizzBuzz question

Posted on 2013-01-11
4
Medium Priority
?
411 Views
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 http://c2.com/cgi/wiki?FizzBuzzTest


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;
        }
        else
        {
        cout << i << endl;
        }
    }
    
    return 0;
}

Open in new window

0
Comment
Question by:Mark_Co
  • 2
4 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 668 total points
ID: 38769358
It's

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.
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 1332 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))
0
 
LVL 31

Accepted Solution

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

Then
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!
0
 

Author Closing Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

876 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