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.
16 lines
301 B
Perl
16 lines
301 B
Perl
5 years ago
|
foreach ( 0 .. 10 ) {
|
||
|
print "$_! = " . factorial( $_ ) . "\n";
|
||
|
}
|
||
|
|
||
|
sub factorial
|
||
|
{
|
||
|
my $number = shift; # get the argument
|
||
|
|
||
|
if ( $number <= 1 ) { # base case
|
||
|
return 1;
|
||
|
}
|
||
|
else { # recursive step
|
||
|
return $number * factorial( $number - 1 );
|
||
|
}
|
||
|
}
|