How to resolve the algorithm Factorial step by step in the Fortran programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Factorial step by step in the Fortran programming language

Table of Contents

Problem Statement

Write a function to return the factorial of a number. Solutions can be iterative or recursive. Support for trapping negative   n   errors is optional.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Factorial step by step in the Fortran programming language

Source code in the fortran programming language

nfactorial = PRODUCT((/(i, i=1,n)/))

INTEGER RECURSIVE FUNCTION RECURSIVE_FACTORIAL(X) RESULT(ANS)
    INTEGER, INTENT(IN) :: X

    IF (X <= 1) THEN
        ANS = 1
    ELSE
        ANS = X * RECURSIVE_FACTORIAL(X-1)
    END IF

END FUNCTION RECURSIVE_FACTORIAL

      INTEGER FUNCTION MFACT(N)
      INTEGER N,I,FACT
      FACT=1
      IF (N.EQ.0) GOTO 20
      DO 10 I=1,N
        FACT=FACT*I
10    CONTINUE
20    CONTINUE
      MFACT = FACT
      RETURN
      END

  

You may also check:How to resolve the algorithm Create an HTML table step by step in the Fortran programming language
You may also check:How to resolve the algorithm Minimum multiple of m where digital sum equals m step by step in the Wren programming language
You may also check:How to resolve the algorithm Square but not cube step by step in the OCaml programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Phix programming language
You may also check:How to resolve the algorithm Yin and yang step by step in the JavaScript programming language