Stut wrote:
> Bruce Gilbert wrote:
>> Thanks for the responses so far.
>>
>> This is what I have come up with
>> [php]
>> <?php
>> for( $i=1; $i<=100; $i++ )
>> {
>> echo $i;
>> echo "<br>";
>> if ($i%3 == 0) echo "Foo ";
>> elseif ($i%5 == 0) echo "Bar";
>> }
>>
>> ?>
>> [/php]
>>
>> and the results can be seen here
>> http://www.inspired-evolution.com/image_test/numbers_output.php
>>
>> I actually want the Foo and Bar to replace the numbers they are
>> dereivatives of and not appear below as they are now, and I also need
>> to add a third condition for "FooBar" which would be for numbers that
>> are both divisible by 3 and 5.
> 
> Ok, this is commonly known as the FizzBuzz problem and is used a lot as
> a university project or interview question. If you can't do it, be afraid!!
> 
> http://dev.stut.net/php/fizzbuzz.php

    $upto = 100;
    $cur = 1;

    for ($cur = 1; $cur <= $upto; $cur++) {
        if ($cur % 3 == 0) {
                print 'Fizz';
                if ($cur % 5 == 0) print 'Buzz';
        } else if ($cur % 5 == 0) {
                print 'Buzz';
        } else {
            print $cur;
        }

        print "<br />";
    }


no need to do each modulo twice per iteration :-)

> 
> -Stut
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to