0% found this document useful (0 votes)
34 views6 pages

Sample Perl Programming Exercises

The document contains 8 sample Perl programs demonstrating basic programming concepts like: 1) Adding two numbers input from the user, 2) Calculating the average of numbers input from the user, 3) Calculating the factorial of a number input from the user, 4) Finding the largest number among numbers input from the user, 5) Examples of if/else statements and ternary operators, 6) Examples of for and while loops, 7) Reversing an array, and 8) Generating a Fibonacci series from a number of terms input from the user.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

Sample Perl Programming Exercises

The document contains 8 sample Perl programs demonstrating basic programming concepts like: 1) Adding two numbers input from the user, 2) Calculating the average of numbers input from the user, 3) Calculating the factorial of a number input from the user, 4) Finding the largest number among numbers input from the user, 5) Examples of if/else statements and ternary operators, 6) Examples of for and while loops, 7) Reversing an array, and 8) Generating a Fibonacci series from a number of terms input from the user.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Sample PERL Programs

1. Add two numbers asking numbers from users

#!/usr/bin/perl

# Made by Pankaj Bajaj for Demonstration

use strict;

use warnings;

print "Enter the first number: ";

my $num1 = <STDIN>;

chomp($num1);

print "Enter the second number: ";

my $num2 = <STDIN>;

chomp($num2);

my $sum = $num1 + $num2;

print "The sum of $num1 and $num2 is: $sum\n";

2. Average of n numbers

#!/usr/bin/perl

# Made by Pankaj Bajaj for Demonstration

use strict;

use warnings;

print "Enter the count of numbers: ";

my $count = <STDIN>;

chomp($count);

my $sum = 0;

for (my $i = 1; $i <= $count; $i++) {


print "Enter number $i: ";

my $num = <STDIN>;

chomp($num);

$sum += $num;

my $average = $sum / $count;

print "The average of the numbers is: $average\n";

3. Factorial of a number

#!/usr/bin/perl

# Made by Pankaj Bajaj for Demonstration

use strict;

use warnings;

print "Enter a number: ";

my $num = <STDIN>;

chomp($num);

my $factorial = 1;

for (my $i = 1; $i <= $num; $i++) {

$factorial *= $i;

print "The factorial of $num is: $factorial\n";

4. Find Largest among n Numbers

#!/usr/bin/perl

# Made by Pankaj Bajaj for Demonstration


use strict;

use warnings;

print "Enter the count of numbers: ";

my $count = <STDIN>;

chomp($count);

my $max;

for (my $i = 1; $i <= $count; $i++) {

print "Enter number $i: ";

my $num = <STDIN>;

chomp($num);

$max = $num if (!defined $max || $num > $max);

print "The largest number is: $max\n";

5. Use of statements

#!/usr/bin/perl

# Made by Pankaj Bajaj for Demonstration

use strict;

use warnings;

# Example of if-else statement

my $x = 10;

if ($x > 5) {

print "x is greater than 5\n";

} else {

print "x is less than or equal to 5\n";


}

# Example of ternary operator

my $y = ($x > 5) ? "x is greater than 5" : "x is less than or equal to 5";

print "$y\n";

6. Use of loops

#!/usr/bin/perl

# Made by Pankaj Bajaj for Demonstration

use strict;

use warnings;

# Example of for loop

print "Counting from 1 to 5 using for loop:\n";

for (my $i = 1; $i <= 5; $i++) {

print "$i ";

print "\n";

# Example of while loop

my $num = 5;

print "Counting down from $num to 1 using while loop:\n";

while ($num >= 1) {

print "$num ";

$num--;

print "\n";
7. Reverse an Array

#!/usr/bin/perl

# Made by Pankaj Bajaj for Demonstration

use strict;

use warnings;

my @array = (1, 2, 3, 4, 5);

print "Original Array: @array\n";

my @reversed_array = reverse @array;

print "Reversed Array: @reversed_array\n";

8. Fibonacci Series

#!/usr/bin/perl

# Made by Pankaj Bajaj for Demonstration

use strict;

use warnings;

print "Enter the number of terms for Fibonacci series: ";

my $terms = <STDIN>;

chomp($terms);

my ($first, $second) = (0, 1);

my @fibonacci_series = ($first, $second);

for (my $i = 2; $i < $terms; $i++) {

my $next = $first + $second;

push @fibonacci_series, $next;

($first, $second) = ($second, $next);


}

print "Fibonacci Series: @fibonacci_series\n";

Common questions

Powered by AI

The script starts by initializing the first two numbers of the Fibonacci series to 0 and 1. It then reads the number of terms the user wishes to generate and proceeds with a loop starting from the third term, where it calculates each subsequent number as the sum of the previous two, appending each new term to a Fibonacci series array. This continues until the required number of terms is generated, and then the entire series is printed .

The script uses both 'for' and 'while' loops for different purposes. A 'for' loop is used to print numbers from 1 to 5 in sequence, with the loop iterating from 1 up to 5 and printing each number during each iteration. It also uses a 'while' loop to count down from a specified number to 1, decrementing the counter and printing each number on each iteration .

The script initializes a maximum variable which is set to undefined. It then iterates over the user-defined number of numbers, comparing each number in turn, updating the maximum variable whenever a number is greater than the current maximum. This strategy ensures that once all numbers have been processed, the maximum variable holds the largest number .

The Perl script creates an array and utilizes Perl's built-in 'reverse' function, which takes an array as input and outputs a new array with the elements in reverse order. This reversed array is then printed, demonstrating a straightforward method to reverse an array using Perl's built-in capabilities .

The Perl script initializes the factorial value to 1 and uses a 'for' loop iterating from 1 up to the number provided by the user. During each iteration, the loop variable is multiplied with the current factorial value to accumulate the product necessary for computing the factorial. This loop-based accumulation continues until the loop completes, resulting in the factorial of the provided number .

If 'strict' and 'warnings' are not enabled, there would be an increased risk of encountering hard-to-diagnose errors, such as typographical errors in variable names or using undeclared variables. These pragmas help catch such issues early, enforce more disciplined code, and improve error detection, ensuring the scripts are more robust and less prone to runtime errors caused by subtle mistakes .

Using an iterative approach for factorial and Fibonacci calculations, as seen in the scripts, generally improves performance and memory usage compared to recursion. Iteration avoids the overhead of multiple function calls and stack allocation, which recursion imposes, especially in languages like Perl where function call overhead might not be optimized away. This leads to more efficient and less error-prone execution, particularly for large inputs which could otherwise lead to stack overflow in a recursive implementation .

The script demonstrates conditional logic using an 'if-else' structure to check if a variable 'x' is greater than 5. If true, it prints a message indicating that 'x' is greater, otherwise, it prints that 'x' is less than or equal to 5. It also employs a conditional ternary operator to achieve a similar logic in a more concise form, using it to assign a string message to another variable 'y' based on the same condition .

The 'chomp' function is used after reading user input to remove the newline character that is automatically appended when the input is read from the standard input (STDIN). This is crucial for processing numeric inputs correctly, as failing to 'chomp' would leave a newline at the end of the input string, which could interfere with arithmetic operations or comparisons .

The Perl script first prompts the user to enter the count of numbers they wish to input. It initializes a sum variable to zero and iterates over the count provided by the user. For each iteration, it reads a number, adds it to the sum, and once all numbers are entered, it calculates the average by dividing the sum by the count of numbers. The calculated average is then printed .

You might also like