Factorial in Programs

Factorial in Lua

1
2
3
4
5
6
7
8
9
10
11
function factorial(n)
if (n == 0) then
return 1
else
return n * factorial(n - 1)
end
end

for n = 0, 16 do
io.write(n, "! = ", factorial(n), "\n")
end

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-- function closures are powerful

-- traditional fixed-point operator from functional programming
Y = function (g)
local a = function (f) return f(f) end
return a(function (f)
return g(function (x)
local c=f(f)
return c(x)
end)
end)
end


-- factorial without recursion
F = function (f)
return function (n)
if n == 0 then return 1
else return n*f(n-1) end
end
end

factorial = Y(F) -- factorial is the fixed point of F

-- now test it
function test(x)
io.write(x,"! = ",factorial(x),"\n")
end

for n=0,16 do
test(n)
end

Factorial in Bash

1
2
3
4
5
6
7
f=1

for (( n=1; $n<=17; $((n++)) ));
do
echo "$((n-1))! = $f"
f=$((f*n))
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
factorial ()
{
local num=$1;
if [ $num = 0 ]; then
echo 1
return ;
fi;
echo $(( $num * $(factorial $(( $num - 1 )) ) ))
}

for ((n = 0; n <= 16; n++))
do
echo "$n! = " $(factorial $(($n)))
done

Factorial in Python

1
2
3
4
5
6
7
8
9
10
11
def factorial(n):
if n == 0:
return 1

f = 1
for i in range(1, n + 1):
f *= i
return f

for n in range(16 + 1):
print "%d! = %d" % (n, factorial(n))

Factorial in Dart

1
2
3
4
5
6
main() {
int fact = 1;
for (int i = 0; i <= 16; ++i, fact *= i) {
print('$i! = $fact');
}
}

Factorial in Java

1
2
3
4
5
6
7
8
9
10
11
public class Factorial {
static long factorial(int n)
{
return ( n==0 ? 1 : n*factorial(n-1) );
}
public static void main(String[] args)
{
for (int n=0; n<=16; n++)
System.out.println(n+"! = "+factorial(n));
}
}

Factorial in PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
function factorial($n)
{
if ($n == 0) {
return 1;
}

return $n * factorial($n - 1);
}

for ($n = 0; $n <= 16; $n++) {
echo $n . "! = " . factorial($n) . "\n";
}
?>

Factorial in Objective-C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

unsigned long long factorial(unsigned long long n)
{
if (n == 0) {
return 1;
} else {
return n * factorial (n - 1);
}
}

int main(void)
{
int n;
for (n = 0; n <= 16; n++) {
printf("%i! = %lld\n", n, factorial(n));
}
return 0;
}

Factorial in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;

printf("Enter an integer: ");
scanf("%d",&n);

// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");

else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

int main() {
int loop;
int factorial=1;
int number = 5;

for(loop = 1; loop<=number; loop++) {
factorial = factorial * loop;
}

printf("Factorial of %d = %d \n", number, factorial);

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*  Example Program For Find Factorial Number In C Programming Language
little drops @ thiyagaraaj.com
Coded By:THIYAGARAAJ MP */

// Header Files
#include<stdio.h>
#include<conio.h>

// define Function and Its Argument Type
long factorial(long);

//Main Function
int main()
{
// Variable Declaration for Factorial Function Input
long f;

printf("Enter the Number for Factorial Calculation :?);
scanf("%ld",&f);
//factorial calculation
printf("The Factorial of %ld is %ld",f,factorial(f));

// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}

// factorial Function
long factorial(long n)
{
int counter;
long fact = 1;

//for Loop for Factorial Calculation Block
for (counter = 1; counter <= n; counter++)
{
fact = fact * counter;
}
return fact;
}

reference