In: Computer Science
(Triangles of Asterisks)
Write a program that displays the following patters separately, one below the other in a TextBox.
Use For..Next loops to generate the patterns. All asterisks (*) should be displayed one at a time by the statement outputTextBox.AppendText(“*”) (this causes the asterisks to display side by side).
The statement outputTextBox.AppendText(vbCrLf) can be used to position to the next line, and a statement of the form outputTextBox.AppendText(“ “) can be used to display spaces for the last two patterns. There should be no other output statements in the program. [Hint: The last two patterns require that each line begin with an appropriate number of blanks.]
Maximize your use of repetition (with nested For…Next statements) and minimize the number of output statements. Set the TextBox’s Font property to Lucida Console, its MultiLine property to True and its ScrollBars property to Vertical so that you can scroll through the results.
(a) | (b) | (c) | (d) |
* ** *** **** ***** ****** ******* ******** ********* ********** | ********** ********* ******** ******* ****** ***** **** *** ** * | ********** ********* ******** ******* ****** ***** **** *** ** * | * ** *** **** ***** ****** ******* ******** ********* ********** |
Public Class Form1
Private Sub btn_DisplayPatterns_Click(sender As Object, e As
EventArgs) Handles btn_DisplayPatterns.Click
Dim r, c, sp As Integer
For r = 1 To 10
For c = 1 To r
OutputTextBox.AppendText("*")
Next c
OutputTextBox.AppendText(vbCrLf)
Next r
OutputTextBox.AppendText(vbCrLf)
OutputTextBox.AppendText(vbCrLf)
For r = 10 To 1 Step -1
For c = 1 To r
OutputTextBox.AppendText("*")
Next c
OutputTextBox.AppendText(vbCrLf)
Next r
OutputTextBox.AppendText(vbCrLf)
OutputTextBox.AppendText(vbCrLf)
For r = 1 To 10
For sp = 1 To r
OutputTextBox.AppendText(" ") 'Comment : one space
Next sp
For c = 10 To r Step -1
OutputTextBox.AppendText("*")
OutputTextBox.AppendText(" ") 'Comment : one space
Next c
OutputTextBox.AppendText(vbCrLf)
Next r
OutputTextBox.AppendText(vbCrLf)
OutputTextBox.AppendText(vbCrLf)
For r = 1 To 10
For sp = 10 To r Step -1
OutputTextBox.AppendText(" ") 'Comment : one space
Next sp
For c = 1 To r
OutputTextBox.AppendText("*")
OutputTextBox.AppendText(" ") 'Comment : one space
Next c
OutputTextBox.AppendText(vbCrLf)
Next r
End Sub
Private Sub btn_exit_Click(sender As Object, e As EventArgs)
Handles btn_exit.Click
Me.Close()
End Sub
End Class