How to resolve the algorithm Department numbers step by step in the Modula-2 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Department numbers step by step in the Modula-2 programming language
Table of Contents
Problem Statement
There is a highly organized city that has decided to assign a number to each of their departments:
Each department can have a number between 1 and 7 (inclusive). The three department numbers are to be unique (different from each other) and must add up to 12. The Chief of the Police doesn't like odd numbers and wants to have an even number for his department.
Write a computer program which outputs all valid combinations.
Possible output (for the 1st and 14th solutions):
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Department numbers step by step in the Modula-2 programming language
Source code in the modula-2 programming language
MODULE DepartmentNumbers;
FROM Conversions IMPORT IntToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
PROCEDURE WriteInt(num : INTEGER);
VAR str : ARRAY[0..16] OF CHAR;
BEGIN
IntToStr(num,str);
WriteString(str);
END WriteInt;
VAR i,j,k,count : INTEGER;
BEGIN
count:=0;
WriteString("Police Sanitation Fire");
WriteLn;
WriteString("------ ---------- ----");
WriteLn;
FOR i:=2 TO 6 BY 2 DO
FOR j:=1 TO 7 DO
IF j=i THEN CONTINUE; END;
FOR k:=1 TO 7 DO
IF (k=i) OR (k=j) THEN CONTINUE; END;
IF i+j+k # 12 THEN CONTINUE; END;
WriteString(" ");
WriteInt(i);
WriteString(" ");
WriteInt(j);
WriteString(" ");
WriteInt(k);
WriteLn;
INC(count);
END;
END;
END;
WriteLn;
WriteInt(count);
WriteString(" valid combinations");
WriteLn;
ReadChar;
END DepartmentNumbers.
You may also check:How to resolve the algorithm Singly-linked list/Element insertion step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Additive primes step by step in the Julia programming language
You may also check:How to resolve the algorithm Numerical integration/Gauss-Legendre Quadrature step by step in the Julia programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the Wren programming language
You may also check:How to resolve the algorithm Pythagoras tree step by step in the Haskell programming language