Friday, December 21, 2012

Recursive Function: Factorial: Part 5: C# Web


Example: Doing Factorial in C# for Web

.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            height: 26px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <br />
        <asp:Panel ID="Panel1" runat="server" Width="410px">
            <table class="style1" bgcolor="#99FF99">
                <tr>
                    <td align="center" class="style2" colspan="2">
                        <strong>Recursion: Factorial Using C# for Web</strong></td>
                </tr>
                <tr>
                    <td align="right" class="style2">
                        <asp:Label ID="lbl01" runat="server" Text=" Put an integer: "></asp:Label>
                    </td>
                    <td align="right" class="style2">
                        <asp:TextBox ID="txb1" runat="server" Width="154px" style="text-align: right"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        &nbsp;</td>
                    <td align="right">
                        <asp:Button ID="btn1" runat="server" onclick="btn1_Click"
                            Text="Click for factorial" Width="160px" />
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        &nbsp;</td>
                    <td align="right">
                        <asp:Label ID="lbl02" runat="server" Text="Factorial anwer will be here"></asp:Label>
                    </td>
                </tr>
            </table>
        </asp:Panel>
        <br />
   
    </div>
    </form>
</body>
</html>
 
 


.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        Factorial f = new Factorial();
        int n;
        if (int.TryParse(txb1.Text, out n))
        {
            lbl02.Text = f.fact(n).ToString();
            txb1.Text = null;
        }
        else
        {
            txb1.Text = null;
            lbl02.Text = "Enter an integer";
        }
    }
}
 


Factorial.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
/// <summary>
/// Summary description for Factorial
/// </summary>
public class Factorial
{
    public Factorial()
       {
       }
    public int fact(int i)
    {
        if (i == 0)
        {
            return 1;
        }
        else
        {
            return i * fact(i - 1);
        }
    }
}
 

Web






No comments:

Post a Comment