How to resolve the algorithm Factorial step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Factorial step by step in the Action! 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 Action! programming language

Source code in the action! programming language

CARD FUNC Factorial(INT n BYTE POINTER err)
  CARD i,res

  IF n<0 THEN
    err^=1 RETURN (0)
  ELSEIF n>8 THEN
    err^=2 RETURN (0)
  FI
    
  res=1
  FOR i=2 TO n
  DO 
    res=res*i
  OD
    
  err^=0
RETURN (res)

PROC Main()
  INT i,f
  BYTE err

  FOR i=-2 TO 10
  DO 
    f=Factorial(i,@err)

    IF err=0 THEN
      PrintF("%I!=%U%E",i,f)
    ELSEIF err=1 THEN
      PrintF("%I is negative value%E",i)
    ELSE
      PrintF("%I! is to big%E",i)
    FI
  OD
RETURN

  

You may also check:How to resolve the algorithm Unicode strings step by step in the Sidef programming language
You may also check:How to resolve the algorithm System time step by step in the Ursa programming language
You may also check:How to resolve the algorithm Mutual recursion step by step in the Lua programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the Comal programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the Factor programming language