QBASIC Program to print the Triangular star pattern (SUB/FUNCTION)

2 years ago

In the world of programming, simplicity can often lead to elegance. Let's take a journey into the realm of BASIC, a programming language known for its straightforward syntax and ease of learning. In ths program we will draw the following traingular star pattern using QBASIC program.

CLS
s$ = "*********"
t = 10
FOR i = 1 TO LEN(s$) STEP 2
     PRINT TAB(t); LEFT$(s$, i)
     t = t - 1
NEXT i
END

Let's break down the code to understand its functionality:

- CLS: Clears the screen, providing a clean slate for our pattern.

- s$ = "*********": Initializes a string s$ with nine asterisks. This string will be the foundation of our pattern.

- t = 10: Initializes a variable t with the value 10. This variable will be used to control the indentation of each line.

- FOR i = 1 TO LEN(s$) STEP 2: Initiates a loop that iterates through the characters of the string s$ with a step of 2. This means it considers every second character in the string.

- PRINT TAB(t); LEFT$(s$, i): Prints a substring of s$ starting from the left and increasing in length by 2 characters in each iteration. The TAB(t) function is used to position the output at column t.

- t = t - 1: Decreases the value of t by 1 in each iteration, shifting the pattern to the left.

- NEXT i: Closes the loop.

- END: Marks the end of the program.

Using SUB Procedure

DECLARE SUB pattern()
CLS
CALL pattern
END

SUB pattern
s$ = "*********"
t = 10
FOR i = 1 TO LEN(s$) STEP 2
     PRINT TAB(t); LEFT$(s$, i)
     t = t - 1
NEXT i
END SUB

 

Using FUNCTION Procedure

DECLARE FUNCTION pattern()
CLS
d = pattern
END

FUNCTION pattern
s$ = "*********"
t = 10
FOR i = 1 TO LEN(s$) STEP 2
     PRINT TAB(t); LEFT$(s$, i)
     t = t - 1
NEXT i
END FUNCTION

 

  1000
Please Login to Post a Comment