How to product this code as shown below:

If I enter "5" and "10" in the console screen, it display
5 *****
10 **********

Whatever I enter any positive numbers, the screen will display the asterisk partern and depend on the number input.

Please tell me the method using VB code to handle this program.

Dani AI

Generated

A simple, robust approach is: read each number from the console, validate it, then produce one output line that contains that many asterisks. pointed toward a loop and Console.Write; correctly noted you can also create the repeated-character string in one call. Validate input with Integer.TryParse to avoid exceptions, treat zero or negative values specially, and avoid allocating huge strings for very large numbers (stream the output instead).

Example (reads one number per line until an empty line):

' Read lines until an empty line; each line should contain a positive integer
Dim line As String = Console.ReadLine()
While Not String.IsNullOrEmpty(line)
    Dim n As Integer
    If Integer.TryParse(line.Trim(), n) AndAlso n > 0 Then
        For i As Integer = 1 To n
            Console.Write("*"c)
        Next
        Console.WriteLine()
    Else
        Console.WriteLine("Invalid input; enter a positive integer or blank line to finish.")
    End If
    line = Console.ReadLine()
End While

Use Integer.TryParse for safe parsing. If you prefer building the whole line at once (for moderate sizes), the String constructor that repeats a character is convenient — see the String constructor docs.

Recommended Answers

All 2 Replies

Since this sounds like an assignment for class, I will try to point you in the right direction and let you finish things off... let me know if you need more help.

1) You will probably want to use a For...Next loop.
2) You probably want to use the commend Console.Write

Instead of a for next loop, use the new string function. ie.

str = New String(character,number of characters)
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.