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

Sample PERL Programs

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)
101 views6 pages

Sample PERL Programs

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
You are on page 1/ 6

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";

You might also like