Friday, 20 February 2015

Sample PERL program to illustrate - 1) Declaring variables 2) Writing to STDOUT, 3) reading user input from STDIN, 4) Using IF statement 5) Using FOR and WHILE loops

#!/usr/local/bin/perl

use strict;
use warnings;

print "What is your name ? ";
my $name = <STDIN>;
print "Hi $name";

print "Enter a number: ";
my $num = <STDIN>;
chomp($num);

print "Even numbers below $num: ";
for(my $i = 1; $i < $num; $i++)
{
  if($i%2 == 0)
  {
    print "$i ";
  }
}
print "\n";

print "Odd numbers below $num: ";
my $j = 1;
while($j < $num)
{
  if($j%2 != 0)
  {
    print "$j ";
  }
  $j++;
}
print "\n";

No comments:

Post a Comment