home

Scientific Computing (Psychology 9040a)

Fall, 2021

Fizz Buzz: sample solution


Here’s one way of doing it:

for i = 1:100
    if ( (mod(i,3)==0) && (mod(i,5)==0) )
        fprintf('FizzBuzz\n');
    elseif (mod(i,3)==0)
        fprintf('Fizz\n');
    elseif (mod(i,5)==0)
        fprintf('Buzz\n');
    else
        fprintf('%d\n', i);
    end
end