How to resolve the algorithm Department numbers step by step in the Fortran programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Department numbers step by step in the Fortran 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 Fortran programming language

Source code in the fortran programming language

      INTEGER P,S,F	!Department codes for Police, Sanitation, and Fire. Values 1 to 7 only.
    1  PP:DO P = 2,7,2	!The police demand an even number. They're special and use violence.
    2   SS:DO S = 1,7		!The sanitation department accepts any value.
    3        IF (P.EQ.S) CYCLE SS	!But it must differ from the others.
    4        F = 12 - (P + S)		!The fire department accepts any number, but the sum must be twelve.
    5        IF (F.LE.0 .OR. F.GT.7) CYCLE SS	!Ensure that the only option is within range.
    6        IF ((F - S)*(F - P)) 7,8,7		!And F is to differ from S and from P
    7        WRITE (6,"(3I2)") P,S,F		!If we get here, we have a possible set.
    8      END DO SS		!Next S
    9    END DO PP	!Next P.
      END	!Well, that was straightforward.


  

You may also check:How to resolve the algorithm Anonymous recursion step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the C++ programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Monkey programming language