Problem: Write a program that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single space or a single carriage return. Maximize your use of repetition (with nested For … Next statements) and minimize the number of output statements.

*
***
*****
*******
*********
*******
*****
***
*

(Diamond Shape)

_________________________________________________________

I think I need 3 nested for loops.

Here is my pseudocode:

For each line
For number of spaces required at beginning of line
write space
Next
For number of stars on line
write star
Next
write new line
Next

_________________________________________________________

I am totally new to programming and this is my firs program. I am having trouble converting my pseudocode into VB .NET code using "For ... Next" Repetition Statements.

I can use Console.WriteLine("*****") but I need to use repetition statements inorder to minimize the number of output statements.

My updated code is this:
Module ForCounter
Sub Main()
Dim i As Integer
Dim j As Integer

For i = 0 To 3
Console.Write("*")
Next
For j = 0 To 5
Console.WriteLine("*")
Next

End Sub ' Main
End Module

Which results in this:

*****
*
*
*
*
*

Clearly not a diamond

How to write three nested For ... Next loops? I am kind of lost here

Please HELP!!!

Recommended Answers

All 2 Replies

Hi

try this:

Dim i As Integer
Dim y As Integer
dim j as integer

For i = -9 To 9
   y = System.Math.Abs(i)
   y = (y * (-1)) + 10
   
    Console.Writeline("")      
       for j=1 to y
        Console.Write("*")
      next

  i += 1
Next

it will write to the console two lines of the 9 stars, but you can include an if statement to avoid this.

hope it helps

If you look at the diamond you will see that each line has two more or two less *'s. So if you start with a string that has one * and print it. Then add two *'s and print again. Do this while the length of the string is less than 10 then start deleting 2 untill there is only one left.

Dim s As String = "*"
        For x As Integer = 0 To 4
            TextBox1.Text &= s & vbCrLf
            s &= "**"
        Next
        s = s.Substring(1, s.Length - 2)
        For x As Integer = 0 To 3
            s = s.Substring(1, s.Length - 2)
            TextBox1.Text &= s & vbCrLf
        Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.