Thursday, December 20, 2012

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:



Recursive Function: Factorial: Part 3: C# Application

Doing Factorial in C# for Windows Application


Here, an example is shown how to implement factorial code in C# for the Windows application.

First, you need to use Design to set up form like this (how to set up components, like button or text box, are not shown).




Recursion Class is shown below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Recursion
{
    class Factorial
    {
        public Factorial()
        {
        }
        public int fact(int n)
        {
            if (n == 0)
            {
                return 1;   //base case
            }
            else
            {
                return n * fact(n - 1);
            }
        }
    }
}


The GUI part is shown below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace Recursion
{
    public partial class txbFactInput : Form
    {
        public txbFactInput()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("testing");
            //int n = Convert.ToInt16(txtb1.Text);
 
            // Using TryParse, test if an integer was entered
            int n;
            if (int.TryParse(txtb1.Text, out n))
            {
                // Integer was put in test box
                txtb1.Text = null;              //clear text box
                Factorial t = new Factorial();  //create Factorial object
                lblAnswer.Text = t.fact(n).ToString();  //answer here
            }
            else
            {
                // Error
                lblAnswer.Text = "Error! Input an integer";
            }
        }
    }
}

The form in Windows Application




Recursive Function: Factorial: Part 1

Factorial


Recursive function is a function that partially calls itself. It is often used for combinatorial search and sorting methods. A simple good example that uses recursive program is factorial, which is mathematically denoted as “n!”

1! = 1
2! = 2 x 1 = 2
3! = 3 x 2 x 1 = 6
4! = 4 x 3 x 2 x 1 = 24
5! = 5 x 4 x 3 x 2 x 1 = 120
6! = 6 x 5 x 4 x 3 x 2 x 1 = 720


 



 
In the above example, the condition, “if n = 0”, is called a base case. When the base case is true, or when the recursion reaches this point, the function finishes and exits.