English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How do I display a triangle in VB using for loop???
Is it something like this:
For i = 1 To 5
Print i;
Print ""
Next i

2007-06-04 22:01:05 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

How do I display a triangle in VB using for loop???
Is it something like this:
For i = 1 To 5
Print i;
Print ""
Next i
This is the output i want:
1
12
123
1234
12345

Please give the exact code

2007-06-04 23:43:02 · update #1

2 answers

Say I want to do this to a text file I would it like this

Dim i As Integer, j As String
Open "c:\mytest.txt" For Output As 1
j = ""
For i = 1 To 5
j = j & i
Print #1, j
Next i
Close #1

I tested it and it works :P - obviously the higher the number on the right the bigger the triangle :)

2007-06-05 17:49:06 · answer #1 · answered by GT 3 · 1 0

*
**
*-*
*--*
*---*
*----*
*-----*
*******

use a for loop to create a series of strings typed line after line
which will for the shape of a triangle. The dashes represent spaces. The easiest way to dothis is to use string concatenation and assemble a string consisting of '*" at each end with a varying amount of spaces determined by the for loop. The peak and base of the triangle are prined before and after the forr loop OR you use an if statement with int the loop to print the peak and base lines which do not have spaces.

2007-06-05 06:29:28 · answer #2 · answered by MarkG 7 · 0 1

fedest.com, questions and answers