0% found this document useful (0 votes)
21 views

Q: Write The Code For Finding The Factorial of A Passed Integer. Use A Recursive Subroutine

The document contains 3 questions and answers about Perl code. The first question asks to write a recursive subroutine to calculate the factorial of a passed integer. The second question asks to write code to find the lowest number in a string of comma separated numbers. The third question asks to write a function to output a diamond shape given an odd integer input.

Uploaded by

Sakthi Vel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Q: Write The Code For Finding The Factorial of A Passed Integer. Use A Recursive Subroutine

The document contains 3 questions and answers about Perl code. The first question asks to write a recursive subroutine to calculate the factorial of a passed integer. The second question asks to write code to find the lowest number in a string of comma separated numbers. The third question asks to write a function to output a diamond shape given an odd integer input.

Uploaded by

Sakthi Vel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Perl

Q: Write the code for finding the factorial of a passed integer. Use a recursive subroutine. A: // BEGIN PERL SNIPET sub factorial { my $y = shift; if ( $y > 1 ) { return $y * &factorial( $y - 1 ); } else { return 1; } } // END PERL SNIPET

Q: Given $a = "5,-3,7,0,-5,12"; Write a program to find the lowest number in the string. A: // BEGIN PERL SNIPET $a = "5,-5,-1,0,12,-3"; (@temp) = split (/,/, $a); $lowest = $temp[0]; for ($i=0; $i<6; $i++) { if ($temp[$i] < $lowest)

{ $lowest = $temp[$i]; } } print "Lowest value found was: $lowest\n"; // END PERL SNIPET NOTE: You could also replace the for loop with this: foreach $value (@temp) { if ($value < $lowest) { $lowest = $value; } }

Q: Write a function to output a diamond shape according to the given (odd) input. Examples: Input is 5 * *** ***** *** * Input is 7 * *** ***** ******* ***** *** *

A: ### BEGIN PERL SNIPET ### for ($i = 1; $i <= (($input * 2) - 1); $i += 2) { if ($i <= $input) { $stars = $i; $spaces = ($input - $stars) / 2; while ($spaces--) { print " "; } while ($stars--) { print "*"; } } else { $spaces = ($i - $input) / 2; $stars = $input - ($spaces * 2); while ($spaces--) { print " "; } while ($stars--) {

print "*"; } } print "\n"; } ### END PERL SNIPET ###

Read more: http://vlsiquestions.webnode.com/perl/ Create your own website for free: http://www.webnode.com

You might also like