You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
417 B
Perl

#!/usr/bin/perl
$number=<>; # read a number from the keyboard
chomp $number; # remove linefeed
$factorial=factorial($number);
# The subroutine
sub factorial {
$input = shift; # read passed argument
return 0 if $input==0;
$result=1;
foreach (1 .. $input) { # '..' generates a range
$result *= $_;
}
return $result;
}
print "$number factorial is $factorial\n";