' Nested Loops — triangle patterns, multiplication table, matrix traversal
#COMPILE EXE
#DIM ALL

SUB TrianglePatterns()
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL outLine AS STRING
    PRINT "=== TRIANGLE PATTERNS ==="
    PRINT "Right triangle (growing):"
    FOR i = 1 TO 8
        outLine = ""
        FOR j = 1 TO i
            outLine = outLine & "*"
        NEXT j
        PRINT outLine
    NEXT i
    PRINT
    PRINT "Right triangle (shrinking):"
    FOR i = 8 TO 1 STEP -1
        outLine = ""
        FOR j = 1 TO i
            outLine = outLine & "*"
        NEXT j
        PRINT outLine
    NEXT i
    PRINT
    PRINT "Pyramid:"
    FOR i = 1 TO 6
        outLine = ""
        FOR j = 1 TO 6 - i
            outLine = outLine & " "
        NEXT j
        FOR j = 1 TO 2 * i - 1
            outLine = outLine & "*"
        NEXT j
        PRINT outLine
    NEXT i
    PRINT
END SUB

SUB MultiplicationTable()
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL outLine AS STRING
    PRINT "=== MULTIPLICATION TABLE (9x9) ==="
    PRINT "   1  2  3  4  5  6  7  8  9"
    PRINT "  +---------------------------"
    FOR i = 1 TO 9
        outLine = STR$(i) & " |"
        FOR j = 1 TO 9
            outLine = outLine & STR$(i * j)
        NEXT j
        PRINT outLine
    NEXT i
    PRINT
END SUB

SUB MatrixTraversal()
    DIM matrix(1 TO 16) AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL sum AS LONG
    LOCAL idx AS LONG
    PRINT "=== MATRIX TRAVERSAL ==="
    FOR i = 1 TO 4
        FOR j = 1 TO 4
            idx = (i - 1) * 4 + j
            matrix(idx) = i * 10 + j
        NEXT j
    NEXT i
    PRINT "Row-major order:"
    FOR i = 1 TO 4
        FOR j = 1 TO 4
            idx = (i - 1) * 4 + j
            PRINT STR$(matrix(idx))
        NEXT j
        PRINT
    NEXT i
    PRINT
    sum = 0
    PRINT "Column-major sum:"
    FOR j = 1 TO 4
        FOR i = 1 TO 4
            idx = (i - 1) * 4 + j
            sum = sum + matrix(idx)
        NEXT i
    NEXT j
    PRINT "Sum = " & STR$(sum)
    PRINT
    PRINT "Main diagonal:"
    FOR i = 1 TO 4
        idx = (i - 1) * 4 + i
        PRINT STR$(matrix(idx))
    NEXT i
    PRINT
    PRINT
END SUB

SUB FloydTriangle()
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL num AS LONG
    LOCAL outLine AS STRING
    PRINT "=== FLOYD'S TRIANGLE ==="
    num = 1
    FOR i = 1 TO 7
        outLine = ""
        FOR j = 1 TO i
            outLine = outLine & FORMAT$(num)
            num = num + 1
        NEXT j
        PRINT outLine
    NEXT i
    PRINT
END SUB

SUB PascalTriangle()
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL outLine AS STRING
    LOCAL val AS LONG
    PRINT "=== PASCAL'S TRIANGLE ==="
    FOR i = 0 TO 7
        outLine = ""
        FOR j = 0 TO 7 - i
            outLine = outLine & "  "
        NEXT j
        val = 1
        FOR j = 0 TO i
            outLine = outLine & FORMAT$(val) & " "
            val = val * (i - j) / (j + 1)
        NEXT j
        PRINT outLine
    NEXT i
    PRINT
END SUB

SUB NumberPatterns()
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL outLine AS STRING
    PRINT "=== NUMBER PATTERNS ==="
    PRINT "Pattern 1 (1 to n per row):"
    FOR i = 1 TO 6
        outLine = ""
        FOR j = 1 TO i
            outLine = outLine & STR$(j)
        NEXT j
        PRINT outLine
    NEXT i
    PRINT
    PRINT "Pattern 2 (repeating row number):"
    FOR i = 1 TO 6
        outLine = ""
        FOR j = 1 TO i
            outLine = outLine & STR$(i)
        NEXT j
        PRINT outLine
    NEXT i
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "======================"
    PRINT " Nested Loops Demo"
    PRINT "======================"
    PRINT
    CALL TrianglePatterns()
    CALL MultiplicationTable()
    CALL MatrixTraversal()
    CALL FloydTriangle()
    CALL PascalTriangle()
    CALL NumberPatterns()
    PRINT "Nested loops complete."
    FUNCTION = 0
END FUNCTION
