Thursday, June 14, 2012

Print * triangle in VBscript


Function printStar()
maxLen = int(InputBox(" Enter a number="))

REM for i = 0 to n
REM print Space(n - i) + String(i, "*") + vbNewLine
REM next

For lineLen = 1 To maxLen
iSpaces = (maxLen - lineLen)
If iSpaces > 0 Then
padSpace = Space(iSpaces / 2)
x = x & padSpace & Replace(Space(lineLen), Space(1), "*") & padSpace & VbCrLf
Else
x = x &  Replace(Space(lineLen), Space(1), "*") & VbCrLf
End If
Next
msgbox x
End Function

O/P: n=5
 
       *
      **
     ***
    ****
   *****

No comments: