5 ms·
Ok. Had to get this out quick... Back to work. Don't know if it will compile <code> #include<iostream> using namespace std; int main() { for(int i = 1; i <= 10
by mipnix 16y ago
Ok. Had to get this out quick...
Back to work.
Don't know if it will compile
<code>
#include<iostream>
using namespace std;
int main()
{
for(int i = 1; i <= 100; ++i)
{
if(i%3==0 &&i%5==0)
cout <<"Fizzbuzz ";
else if(i % 3==0)
cout <<"FIzz ";
else if(i%5==0)
cout<<"buzz ";
else
cout << i << " ";
}
return 0;
}
</code>
- RiderOfGiraffes 16y ago#include<iostream> using namespace std; int main() { for(int i = 1; i <= 100; ++i) { if(i%3==0 && i%5==0) cout <<"Fizzbuzz "; else if(i%3==0) cout <<"FIzz "; else if(i%5==0) cout<<"buzz "; else cout << i << " "; } return 0; } Cool. Assuming you knocked that out as quickly as you suggest it shows that you've got good basic skills. Design and control of larger tasks/projects is untested, but it looks like you're well ahead of the basics. Now here's a question: Do you find anything "unsatisfactory" about that code?
- mipnix 16y agoBesides not knowing how to post it properly...? The looping sequence, I imagine, doesn't need the if else queries. Probably simpler way to code it. I am definitely untested with larger projects.
- DEADBEEF 16y agoSomething like... (Pseudocode) for( i= 1,100 ) { if( i%3==0 ) { print( 'Fizz' ) } if( i%5==0 ) { print( 'Buzz' ) } print( '\n' ) } There's no need to treat FizzBuzz as a special case, as if the number is divisible by both it will have already met the conditions of the previous two statements. I wonder if there's some magic you could sprinkle in to the iterator so it only iterates through numbers which are divisible by 3/5, ignoring the rest? It probably wouldn't speed the operation up much (if at all) in this case, in a more complex real world scenario though it's best to look at every angle.
- RiderOfGiraffes 16y agoYou haven't printed any of the other numbers. Read the spec more carefully. You still need to print 1, 2, 4, etc.