General FizzBuzz: Difference between revisions
Content added Content deleted
(Added an example for D) |
|||
Line 651: | Line 651: | ||
finally (fizzbuzz (parse-integer input :junk-allowed t) (read-factors)))) |
finally (fizzbuzz (parse-integer input :junk-allowed t) (read-factors)))) |
||
</lang> |
</lang> |
||
=={{header|D}}== |
|||
<lang D>import core.stdc.stdlib; |
|||
import std.stdio; |
|||
void main() { |
|||
int limit; |
|||
write("Max number (>0): "); |
|||
readf!"%d\n"(limit); |
|||
if (limit <= 0) { |
|||
writeln("The max number to consider must be greater than zero."); |
|||
exit(1); |
|||
} |
|||
int terms; |
|||
write("Terms (>0): "); |
|||
readf!"%d\n"(terms); |
|||
if (terms <= 0) { |
|||
writeln("The number of terms to consider must be greater than zero."); |
|||
exit(1); |
|||
} |
|||
int[] factors = new int[terms]; |
|||
string[] words = new string[terms]; |
|||
for (int i=0; i<terms; ++i) { |
|||
write("Factor ", i+1, " and word: "); |
|||
readf!"%d %s\n"(factors[i], words[i]); |
|||
if (factors[i] <= 0) { |
|||
writeln("The factor to consider must be greater than zero."); |
|||
exit(1); |
|||
} |
|||
} |
|||
foreach(n; 1..limit+1) { |
|||
bool print = true; |
|||
for (int i=0; i<terms; ++i) { |
|||
if (n % factors[i] == 0) { |
|||
write(words[i]); |
|||
print = false; |
|||
} |
|||
} |
|||
if (print) { |
|||
writeln(n); |
|||
} else { |
|||
writeln(); |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Max number (>0): 20 |
|||
Terms (>0): 3 |
|||
Factor 1 and word: 3 Fizz |
|||
Factor 2 and word: 5 Buzz |
|||
Factor 3 and word: 7 Baxx |
|||
1 |
|||
2 |
|||
Fizz |
|||
4 |
|||
Buzz |
|||
Fizz |
|||
Baxx |
|||
8 |
|||
Fizz |
|||
Buzz |
|||
11 |
|||
Fizz |
|||
13 |
|||
Baxx |
|||
FizzBuzz |
|||
16 |
|||
17 |
|||
Fizz |
|||
19 |
|||
Buzz</pre> |
|||
=={{header|Elixir}}== |
=={{header|Elixir}}== |