Playing with StringBuilder on C#
string Diamond(int i) { if (i < 0 || i % 2 == 0) return null; var sb = new StringBuilder().Append(Environment.NewLine).Append('*', i).Append(Environment.NewLine); for (int x = i; i >= 1; --x) { sb.Append(' ', x - i).Append('*', i).Append(Environment.NewLine).Insert(0, "*", i).Insert(0, " ", x - i).Insert(0, Environment.NewLine); i -= 2; } return sb.ToString(); }
And Visual Basic.NET
Private Function Diamond(ByVal i As Integer) As String If i < 0 OrElse i Mod 2 = 0 Then Return Nothing Dim sb = New StringBuilder().Append(Environment.NewLine).Append("*"c, i).Append(Environment.NewLine) Dim x = i i -= 2 While i >= 1 x -= 1 sb.Append(" "c, x - i).Append("*"c, i).Append(Environment.NewLine).Insert(0, "*", i).Insert(0, " ", x - i).Insert(0, Environment.NewLine) i -= 2 End While Return sb.ToString() End Function
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Playing with StringBuilder on C#
And Visual Basic.NET