116k views
1 vote
Write a program in QBASIC to display the following pattern (a) 1 22 333 4444 5555 (b) 99999 7777 5 55 33 1 (c) 1 10 101 1010 10101 ​

1 Answer

5 votes

Final Answer:

qbasic

' Pattern (a)

FOR i = 1 TO 5

FOR j = 1 TO i

PRINT i;

NEXT j

PRINT

NEXT i

' Pattern (b)

FOR i = 5 TO 1 STEP -1

FOR j = 1 TO i

PRINT i;

NEXT j

PRINT

NEXT i

' Pattern (c)

FOR i = 1 TO 5

FOR j = 1 TO i

PRINT j MOD 2;

NEXT j

PRINT

NEXT i

Step-by-step explanation:

In the first pattern (a), the outer loop iterates from 1 to 5, representing the number of rows. The inner loop prints the value of the outer loop variable (i) for each iteration, creating the desired pattern.

For the second pattern (b), the outer loop starts from 5 and decreases by 1 in each iteration until 1. The inner loop prints the value of the outer loop variable (i) for each iteration, forming the inverted pyramid pattern.

In the third pattern (c), the outer loop represents the number of rows, and the inner loop prints the value j MOD 2.

The modulo operation alternates between 0 and 1, creating a binary pattern where each row has an increasing number of alternating 0s and 1s.

These QBASIC programs utilize nested loops to control the row and column structure, ensuring the correct output patterns.

The first two patterns involve simple counting, while the third pattern employs the modulo operation to create a binary sequence. The choice of loop structures and print statements is crucial to achieve the specified patterns accurately.

User Underverse
by
8.0k points