How to resolve the algorithm General FizzBuzz step by step in the Modula-2 programming language
How to resolve the algorithm General FizzBuzz step by step in the Modula-2 programming language
Table of Contents
Problem Statement
Write a generalized version of FizzBuzz that works for any list of factors, along with their words. This is basically a "fizzbuzz" implementation where the user supplies the parameters. The user will enter the max number, then they will enter the factors to be calculated along with the corresponding word to be printed. For simplicity's sake, assume the user will input an integer as the max number and 3 factors, each with a word associated with them.
For example, given: In other words: For this example, print the numbers 1 through 20, replacing every multiple of 3 with "Fizz", every multiple of 5 with "Buzz", and every multiple of 7 with "Baxx". In the case where a number is a multiple of at least two factors, print each of the words associated with those factors in the order of least to greatest factor. For instance, the number 15 is a multiple of both 3 and 5; print "FizzBuzz". If the max number was 105 instead of 20, you would print "FizzBuzzBaxx" because it's a multiple of 3, 5, and 7.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm General FizzBuzz step by step in the Modula-2 programming language
Source code in the modula-2 programming language
MODULE GeneralFizzBuzz;
FROM Conversions IMPORT StrToInt;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT Write,WriteString,WriteLn,ReadChar;
TYPE
Word = ARRAY[0..63] OF CHAR;
PROCEDURE WriteInt(i : INTEGER);
VAR buf : Word;
BEGIN
FormatString("%i", buf, i);
WriteString(buf);
END WriteInt;
PROCEDURE ReadInt() : INTEGER;
VAR
buf : ARRAY[0..9] OF CHAR;
c : CHAR;
i : INTEGER;
BEGIN
i := 0;
LOOP
c := ReadChar();
IF (c=0C) OR (i>9) THEN
BREAK
ELSIF (c=012C) OR (c=015C) THEN
WriteLn;
buf[i] := 0C;
BREAK
ELSIF (c<'0') OR (c>'9') THEN
Write(c);
buf[i] := 0C;
BREAK
ELSE
Write(c);
buf[i] := c;
INC(i)
END
END;
StrToInt(buf, i);
RETURN i
END ReadInt;
PROCEDURE ReadLine() : Word;
VAR
buf : Word;
i : INTEGER;
c : CHAR;
BEGIN
i := 0;
WHILE i
c := ReadChar();
IF (c=0C) OR (c=012C) OR (c=015C) THEN
WriteLn;
buf[i] := 0C;
BREAK
ELSE
Write(c);
buf[i] := c;
INC(i)
END
END;
RETURN buf;
END ReadLine;
VAR
i,max : INTEGER;
fa,fb,fc : INTEGER;
wa,wb,wc : Word;
done : BOOLEAN;
BEGIN
max := ReadInt();
fa := ReadInt();
wa := ReadLine();
fb := ReadInt();
wb := ReadLine();
fc := ReadInt();
wc := ReadLine();
FOR i:=1 TO max DO
done := FALSE;
IF i MOD fa = 0 THEN
done := TRUE;
WriteString(wa);
END;
IF i MOD fb = 0 THEN
done := TRUE;
WriteString(wb);
END;
IF i MOD fc = 0 THEN
done := TRUE;
WriteString(wc);
END;
IF NOT done THEN
WriteInt(i)
END;
WriteLn;
END;
ReadChar
END GeneralFizzBuzz.
You may also check:How to resolve the algorithm HTTPS/Client-authenticated step by step in the Java programming language
You may also check:How to resolve the algorithm Soloway's recurring rainfall step by step in the Ruby programming language
You may also check:How to resolve the algorithm Matrix transposition step by step in the Hope programming language
You may also check:How to resolve the algorithm AKS test for primes step by step in the Ruby programming language
You may also check:How to resolve the algorithm Pell's equation step by step in the Delphi programming language