codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <iostream> // Prints numbers "1" through "100" as integers. // Every multiple of "3" is printed as "Fizz". // Every multiple of "5" is printed as "Buzz". // Every common multiple of "3" and "5" is printed as "FizzBuzz". // for loops evaluate whether a quotient is an integer. int main() { float counterMain = 1; // factorMain will be set to the multiples for Fizz or "Buzz. float factorMain = 0; // factorTest "tests" factorMain to see if it is an integer factor of counterMain. float factorTest = 1; // factorLimit sets the divisor (factor) upper limit (only one other factor in this case). // factorLimit is a quotient of factorMain and factorTest. float factorLimit = 0; int passFizz = 0; int passBuzz = 0; // counterMain gets printed at the end if nothing is passed to checkFizzBuzz. for(counterMain = 1; counterMain <= 100; ++counterMain) { // Resets pass check. passFizz = 0; passBuzz = 0; // "Fizz" value set here. factorMain = 3; // Sets the divisor upper limit as a quotient of counterMain. factorLimit = counterMain / factorMain; //factorTest counts to the divisor upper limit. for(factorTest = 1; factorTest <= factorLimit; ++factorTest) { // Tests if factorTest is an integer quotient of counterMain divided by factorMain. if(factorTest == factorLimit) { // Passes 1 to passFizz if factorMain is an integer. passFizz = 1; // Sets factorTest to for loop exit condition. factorTest = factorLimit; } } // "Buzz" value is set here. factorMain = 5; factorLimit = counterMain / factorMain; // Copy of the above for loop but passes 2 to passBuzz instead. for(factorTest = 1; factorTest <= factorLimit; ++factorTest) { if(factorTest == factorLimit) { passBuzz = 2; factorTest = factorLimit; } } // Fizz-Buzz pass check, then prints out. int checkFizzBuzz = (passFizz + passBuzz); // Value of 0 means no factors were found, prints counterMain value instead. if(checkFizzBuzz == 0) { std::cout << counterMain; } if(checkFizzBuzz == 1) { std::cout << "Fizz"; } if(checkFizzBuzz == 2) { std::cout << "Buzz"; } if(checkFizzBuzz == 3) { std::cout << "FizzBuzz"; } std::cout << "\n"; // Return to counterMain for loop. } return 0; }
Private
[
?
]
Run code
Submit