Section Arrays: Compute Factorial#
Adapted from: gjbex/Fortran-MOOC
This program demonstrates computing the factorials over an array of integers in Fortran.#
program compute_factorial
implicit none
integer :: i
integer, dimension(5) :: values = [ (i, i=1,5) ]
print *, values
print *, factorial(values)
contains
elemental integer function factorial(n)
implicit none
integer, value :: n
integer :: i
factorial = 1
do i = 2, n
factorial = factorial*i
end do
end function factorial
end program compute_factorial
The above program is compiled and run using Fortran Package Manager (fpm):
import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Fortran_Code/Section_Arrays_Compute_Factorial"
os.chdir(code_dir)
build_status = os.system("fpm build 2>/dev/null")
exec_status = os.system("fpm run 2>/dev/null")
1 2 3 4 5
1 2 6 24 120