Example Code

VI Recursion - Factorial (LabVIEW 2012)

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Download All

Description

 Overview

This example uses the new drag and drop recursion interface to calculate the Factorial of a given number N

 

Requirements

LabVIEW Base Development System 2012 or compatible

 

Steps to Implement or Execute Code

  1. Enter the value to be calculated in "n" control
  2. Run the VI

Description

This example uses the new drag and drop recursion interface to calculate the Factorial of a given number N. This VI uses a standard algorithm that can be found on wikipedia.

 

 Additional Information or References

VI Block Diagram

6177.PNG

 

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
ghaddou
Member
Member
on

it doesn't work for number higher than 5

HunterSeeker
Member
Member
on

I uploaded a new version that uses U32 integer data types, this will allow it to work for bigger numbers. The previous version was limited because it was the i8 data type (-128 to 127)

StaceyG
Member
Member
on

0! is not handled correctly. 0! is defined as 1. See various links:

http://mathforum.org/library/drmath/view/57905.html

http://wiki.answers.com/Q/Why_is_zero_factorial_equal_to_one

http://en.wikipedia.org/wiki/Factorial#Definition

 

 

A simple way to fix your VI:

1. Change the >1 to >0.

2. In the TRUE case, hard-wire a 1 to the output.