RECURSIVE

 !Program name: RECURSIVE

!calculate the factorial of a number n!

program recursive

 

   integer :: i, f

   character (len=1) :: answer

   Print *, 'Input the value to be factorized: '

   Read *, i

! Variable function:

   f = factorial(i)

   Print *, 'The value of factorial of', i, ' 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 *, i

        f = factorial(i)

        Print *, 'The value of the factorial of ', i, ' is: '

        Print *, f

        Go to 10

      else

      print *, 'Ok. Goodbye.'

   end if

 

end program recursive

 

! computes the factorial of n (n!)

recursive function factorial (n) result (fac)

! function result

implicit none

 

   ! dummy arguments

   integer :: fac

   integer, intent (in) :: n

 

   select case (n)

      case (0:1)

         fac = 1

      case default

         fac = n * factorial (n-1) !the function is called recursively here.

   end select

 

end function factorial

Comentários

Postagens mais visitadas deste blog

HYDROGEN-RADIAL

HYDROGEN-POTENTIAL

ONE_PART_QHO