Print numbers from 1 to n using recursion in vb.net

Vb program for Print numbers from 1 to n using recursion. Here more solutions.

' Include namespace system
Imports System 
'  Vb.net program for
'  Print numbers from 1 to n using recursion
public Class Numbers
    Public Sub printNumber(ByVal num As Integer)
        if (num >= 1) Then
            '  Reduce the number and try again 
            '  Until n is greater than zero
            Me.printNumber(num - 1)
            '  Display calculated result
            Console.Write(" " + num.ToString())
        End If
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As Numbers = New Numbers()
        '  Test A (1..10)
        Dim num As Integer = 10
        Console.Write( vbLf &" (1 to " + num.ToString() + ") : ")
        task.printNumber(num)
        '  Test B (1..20)
        num = 20
        Console.Write( vbLf &" (1 to " + num.ToString() + ") : ")
        task.printNumber(num)
    End Sub
End Class

Output

 (1 to 10) :  1 2 3 4 5 6 7 8 9 10
 (1 to 20) :  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved