Do the following two questions:

  1. Write a Fortran 90 FUNCTION which computes N factorial (i.e. N!). Input: a positive integer N. Returns: N Factorial.

 

Solution:

 

FUNCTION Factorial(N)

 

IMPLICIT NONE

INTEGER :: Factorial, I

INTEGER, INTENT(IN) :: N

 

DO I=1, N

            Factorial=Factorial*I

END DO

 

END FUNCTION Factorial

 

 

  1. Write a Fortran 90 SUBROUTINE which computes the sum of the integers greater than or equal to 1 and less than or equal to N. Input: a positive integer N. Returns: the sum of the integers greater than or equal to 1 and less than or equal to N.

 

Solution:

 

SUBROUTINE Sum_Sub(N, MySum)

 

      IMPLICIT NONE

      INTEGER, INTENT(IN) :: N

      INTEGER, INTENT(OUT) :: MySum

      INTEGER :: I

     

MySum=0

      DO I=1, N

            MySum=MySum+I

      END DO

 

END SUBROUTINE Sum_Sub