RECURSIVE2
!Program name: RECURSIVE2
program
factorial
integer :: n, f
character (len=1) :: answer
Print *, 'Input the value to be factorized:
'
Read
*, n
f = factorial(n)
Print *, 'The value of
factorial of', n, ' is: '
Print *, f
10 Print *, 'Do you want to calculate another
factorial (Y/N)?'
Read *, answer
If (answer == 'Y') then
Print *, 'Input another value to be
factorized: '
Read *, n
f = factorial(n)
Print *, 'The value of the
factorial of ', n, ' is: '
Print *, f
Go to 10
else
print *, 'Ok. Goodbye.'
end if
end program factorial
! computes the factorial of n
(n!)
recursive function factorial
(n) result (f)
! function result
implicit none
! dummy arguments
integer :: f
integer, intent(in) :: n
If (n == 0) then
f = 1
else
f = n * factorial (n-1) !the function is called recursively here
end if
end function factorial
Comentários
Postar um comentário