# File: fact1.pl # Title: FACTORIAL n! # Use subroutine. Input from keyboard an integer n. Compute factorial # Author: Mihaela Malita # Input number? 5 # Result: 5! = 120 # Continue(y/n)? n # Bye $yesorno = "y"; while($yesorno eq "y") { print "Input number? "; $n = ; chop($n); print "\nResult: $n ! = $r",fact($n); # invoke subroutine fact($n) print "\nContinue(y/n)? "; $yesorno = ; chop($yesorno); } print "Bye"; exit; sub fact { # subroutine called fact() my($n) = @_; # n is local variable, @_ is the array of arguments of fact(@_) my($r) = 1; #initialise result for ($i = 1; $i < $n ; $i++) { $r = $r * $i; # each time you multiply with another $i } return $r; }