How to compute the factorial of a number greater than 20 in VB.NET

1 Answer

0 votes
Imports System
Imports System.Numerics

' 
'   This program computes the factorial of numbers greater than 20.
'   VB.NET provides the BigInteger type, which supports arbitrary‑precision
'   arithmetic and is ideal for very large factorials.
'

Module BigFactorial

    '
    '   Compute factorial using BigInteger.
    '   The algorithm multiplies numbers from 2 to n.
    '   BigInteger handles overflow internally and grows as needed.
    '
    Function FactorialBig(n As Integer) As BigInteger
        Dim result As BigInteger = BigInteger.One

        For i As Integer = 2 To n
            result *= i
        Next

        Return result
    End Function

    '
    '   Main entry point: read input, compute factorial, print result.
    '
    Sub Main()
        Console.Write("Enter a number greater than 20: ")
        Dim n As Integer = Integer.Parse(Console.ReadLine())

        Dim result As BigInteger = FactorialBig(n)

        Console.WriteLine()
        Console.WriteLine("Factorial of " & n & " is:")
        Console.WriteLine()
        Console.WriteLine(result)
    End Sub

End Module


'
' run:
'
' Enter a number greater than 20: 25
' 
' Factorial of 25 is:
' 
' 15511210043330985984000000
'

 



answered Sep 13 by avibootz
...