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

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

SUB MultiplicationTable()
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL line AS STRING
    PRINT "=== MULTIPLICATION TABLE (9x9) ==="
    PRINT "   1  2  3  4  5  6  7  8  9"
    PRINT "  +---------------------------"
    FOR i = 1 TO 9
        line = STR$(i) & " |"
        FOR j = 1 TO 9
            line = line & FORMAT$(i * j, "###")
        NEXT j
        PRINT line
    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 line AS STRING
    PRINT "=== FLOYD'S TRIANGLE ==="
    num = 1
    FOR i = 1 TO 7
        line = ""
        FOR j = 1 TO i
            line = line & FORMAT$(num, "###")
            num = num + 1
        NEXT j
        PRINT line
    NEXT i
    PRINT
END SUB

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

SUB NumberPatterns()
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL line AS STRING
    PRINT "=== NUMBER PATTERNS ==="
    PRINT "Pattern 1 (1 to n per row):"
    FOR i = 1 TO 6
        line = ""
        FOR j = 1 TO i
            line = line & STR$(j)
        NEXT j
        PRINT line
    NEXT i
    PRINT
    PRINT "Pattern 2 (repeating row number):"
    FOR i = 1 TO 6
        line = ""
        FOR j = 1 TO i
            line = line & STR$(i)
        NEXT j
        PRINT line
    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
