Friday, December 21, 2012

Recursive Function: Factorial: Part 6: HTML and JavaScript


Example: Doing Factorial in HTML and JavaScript



<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example: Doing Factorial in HTML with JavaScript</title>
<script type="text/javascript">
    function calcFact() {
 
        // do below for lable, p, h2, etc.
        // means, need no .value for rectrieving data
        //        need    .innerHTML for outputting data
        //var x = document.getElementById("lbl01");
        //alert(x.innerHTML);
 
        // do below for textbox
        // means, need    .value for rectrieving data
        //        need no .innerHTML for outputting data     
        //var y =document.getElementById("txb01").value;
        //alert(y);
        //   or do this
        //alert(document.getElementById("txb01").value);
 
        var x = document.getElementById("txb01").value;
        if (x == parseInt(x)) {
            // Number was integer
            document.getElementById("lbl02").innerHTML = fact(x);
        }
        else {
            document.getElementById("lbl02").innerHTML = 'Empty or not integer';
        }
    }
    function fact(i) {
        if (i == 0) {
            return 1;
        }
        else {
            return i * fact(i - 1);
        }
    }
</script>
</head>
 
<body>
    <h2>Example: Doing Factorial in HTML with JavaScript</h2>
    <label id="lbl01">Enter integer here: </label>
    <input id="txb01" type="text" />
    <input id="btn01" type="button" onclick="calcFact()" value="Calculate Factorial" />
    <label id="lbl02">The answer will be posted here</label>
 
</body>
</html>
 

 
Webpage



 

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






Thursday, December 20, 2012

Recursive Function: Factorial: Part 4: C++ Console Application

Doing Factorial in C++ for Console Application



Factorial.h

#ifndef FACTORIAL_H
#define FACTORIAL_H
#pragma once
 
class Factorial
{
       public:
              Factorial();
              int fact(int n);
              ~Factorial(void);
};
#endif


Factorial.cpp

#include "StdAfx.h"
#include "Factorial.h"
 
Factorial::Factorial()     // constructor
{
}
 
int Factorial::fact(int n)
{
       if (n == 0)
       {
              return 1;
       }
       else
       {
              return n * fact(n-1);
       }
}
 
Factorial::~Factorial()
{
}
 


Recursion.cpp

// Recursion.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Factorial.h"
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
       Factorial f;  //created object of Factorial
 
       // calculate and print "3!"
       cout << f.fact(3) << endl;
       return 0;
}
 


Console output




Recursive Function: Factorial: Part 2: Java

Doing Factorial in Java



/*
 * Factorial returns the factorial n!
 * 2012.12.19
 * Tetsuro
 */
 
public class Factorial {
 
       // constructor for Factorial class
       public Factorial() {
             
       }
 
       // this method returns n!
       public int fact(int n) {
              if (n == 0) {
                     return 1;
              }
              else {
                     return n * fact(n - 1);           // fact(n - 1) always returns 1
              }
       }
}



public class RecursionTest {
      
       /**
        * Program for testing factorial object
        */
      
       public static void main(String[] args) {
             
              // instantiating f from Factorial class
              // created object f
              Factorial f = new Factorial();
             
              // run the factorial f with passing integer 5
              // and print the result
              System.out.print(f.fact(5));
       }
}



Example:


Factorial, “3!” works like this: