% FizzBuzz - print all numbers from 1 to 100, replacing multiples of 3 with
% "fizz", multiples of 5 with "buzz", and multiples of 3 and 5 with
% "fizzbuzz".
clear
clc
for i = 1:100
fb = '';
if length(find(factor(i)==3)) > 0
fb = [fb 'fizz'];
end
if length(find(factor(i)==5)) > 0
fb = [fb 'buzz'];
end
if length(fb) > 0
fprintf([fb '\n'])
else
fprintf('%5.0f\n', i)
end
end
A better program (by which I mean “faster”, not “clearer” or “easier to modify” or “easier to maintain”) would replace the tests with something less intensive—for example, incrementing two counters (one for 3 and one for 5) and zeroing them when they hit their respective desired factors.
As everyone else seems to be posting their code:
A better program (by which I mean “faster”, not “clearer” or “easier to modify” or “easier to maintain”) would replace the tests with something less intensive—for example, incrementing two counters (one for 3 and one for 5) and zeroing them when they hit their respective desired factors.