Advertisement
pcwizz

recursive calculation of factorials

Apr 20th, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.47 KB | None | 0 0
  1. #!/bin/perl
  2. #factorials from standard input
  3. sub factorial{
  4.     if(@_ != 1){
  5.         print "Warning! More/Less than one parameter to the factorial subroutine";
  6.     }
  7.     if ($_[0] == 1) {
  8.         return 1;# force a return to go back up the stack
  9.     }
  10.     $_[0] * factorial($_[0]-1);# no need for the return keyword, last thing done so at the top of the stack
  11. }
  12.  
  13. while (<>) {
  14.     chomp;
  15.     $factorialn = &factorial($_);
  16.     print "Factorial " . $_ . " or " . $_ . "! is equal to " . $factorialn . "\n";
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement