Factorial in Basic
Example for versions
QBasic 1.1,
QuickBASIC 4.5
This example uses recursive factorial definition.
The default data type for calculations is floating point, so program output looks like this:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 3.99168Е+07
12! = 4.790016Е+08
13! = 6.227021Е+09
14! = 8.717829Е+10
15! = 1.307674Е+12
16! = 2.092279Е+13
DECLARE FUNCTION factorial (n)
FOR i = 0 TO 16:
PRINT STR$(i) + "! =" + STR$(factorial(i))
NEXT i
END
FUNCTION factorial (n)
IF n = 0 THEN
factorial = 1
ELSE
factorial = n * factorial(n - 1)
END IF
END FUNCTION
Comments
]]>blog comments powered by Disqus
]]>