How to resolve the algorithm Sierpinski triangle step by step in the COBOL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sierpinski triangle step by step in the COBOL programming language

Table of Contents

Problem Statement

Produce an ASCII representation of a Sierpinski triangle of order   N.

The Sierpinski triangle of order   4   should look like this:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sierpinski triangle step by step in the COBOL programming language

Source code in the cobol programming language

identification division.
program-id. sierpinski-triangle-program.
data division.
working-storage section.
01  sierpinski.
    05 n              pic 99.
    05 i              pic 999.
    05 k              pic 999.
    05 m              pic 999.
    05 c              pic 9(18).
    05 i-limit        pic 999.
    05 q              pic 9(18).
    05 r              pic 9.
procedure division.
control-paragraph.
    move 4 to n.
    multiply n by 4 giving i-limit.
    subtract 1 from i-limit.
    perform sierpinski-paragraph
    varying i from 0 by 1 until i is greater than i-limit.
    stop run.
sierpinski-paragraph.
    subtract i from i-limit giving m.
    multiply m by 2 giving m.
    perform m times,
    display space with no advancing,
    end-perform.
    move 1 to c.
    perform inner-loop-paragraph
    varying k from 0 by 1 until k is greater than i.
    display ''.
inner-loop-paragraph.
    divide c by 2 giving q remainder r.
    if r is equal to zero then display '  * ' with no advancing.
    if r is not equal to zero then display '    ' with no advancing.
    compute c = c * (i - k) / (k + 1).


  

You may also check:How to resolve the algorithm Date format step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the Rust programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the ChucK programming language
You may also check:How to resolve the algorithm Queue/Usage step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the Factor programming language