Create a class library with function Factorial() - ASP.NET Program

Create a “class library” with a function Factorial() which calculates and returns factorial of a given integer number. Use this “class library” in ASP.NET application.

Answer:

Introduction:
A class library contains the library of classes, interfaces and value types that provide the access to the certain functionalities. Classes provide the reusable code in form of object.

In the following application we create a class library with function Factorial(), which calculates the factorial of the given number. The name of the class library is “ClassLibraryDemoTest”.

The code is written in Visual Basic.

Default.aspx.vb

Imports ClassLibraryDemoTest
Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
    Private Function Factorial(ByVal n As Integer)
        If (n = 0) Then
            Factorial = 1
        Else
            Factorial = n * Factorial(n - 1)
        End If
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
        Dim n As Integer
        n = Integer.Parse(TextBox1.Text)
        Label1.Text = Factorial(n).ToString()
    End Sub
End Class


Default.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="ClassLibraryDemoTest._Default" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
    <body>
         <form id="form1" runat="server">
               <div>
                     <fieldset style="width:380px; background-color:antiquewhite">
                            Enter Number: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                            <asp:Button ID="btn" runat ="server" Text="Factorial" OnClick ="Page_Load"/>  
                            <asp:Label ID="Label1" runat="server" Text =""></asp:Label>
                     </fieldset>
               </div>
          </form>
     </body>
</html>


Output:

1. Factorial of 5

factorial of 5

2. Factorial of 6

factorial of 6