Write a program in VB.net to find the factorial of a number using the Yield Return Statement

Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 2877
The following program will do so

Imports System.Collections.Generic

Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(args As String())
Dim input As Integer = 7
'Factorial of the number to find
Dim fact As Integer = 1
'a temporary variable to store the intermediate computed values
For Each number As var In Factorial(input)
fact *= number

'display the result
Console.WriteLine("Factorial of {0} is {1}", input, fact)
End Sub

'generate the range of numbers - one at each cycle
Public Shared Function Factorial(number As Integer) As IEnumerable(Of Integer)
Dim startIndex As Integer = 1

'loop continues till the condition becomes false
While startIndex <= number
yield Return startIndex
startIndex += 1
End While
End Function
End Class
End Namespace

Comments or Responses

Login to post response