[ create a new paste ] login | about

Link: http://codepad.org/AnOA5PFC    [ raw code | output | fork ]

C++, pasted on Oct 11:
#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;
}


Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz


Create a new paste based on this one


Comments: