Quote: "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". "
Is this the fastest way?
#include <iostream>
int main( int argc, char* argv[] )
{
// count from 1 to 100
for( int x = 1; x != 101; x++ )
{
// process
for(;;)
{
// divisible by 3
if( x%3 == 0 )
{
std::cout << "Fizz";
// also divisible by 5
if( x%5 == 0 )
{
std::cout << "Buzz";
}
// end line and break
std::cout << std::endl;
break;
}
// divisible by 5
if( x%5 == 0) )
{
std::cout << "Buzz" << std::endl;
break;
}
// output number
std::cout << x << std::endl;
break;
}
}
return 0;
}
[EDIT] Obviously, this code is shorter, but it does the division twice to check its divisibility.
#include <iostream>
int main( int argc, char* argv[] )
{
// count from 1 to 100
for( int x = 1; x != 101; x++ )
{
if( x%3 == 0 ) std::cout << "Fizz";
if( x%5 == 0 ) std::cout << "Buzz";
if( (x%3) && (x%5) ) std::cout << x;
std::cout << std::endl;
}
return 0;
}
TheComet

Level 91 Forumer - 9600 health, 666'666 keystroke power (*2 coffee)
Abilities: sophisticated troll, rage