SIMPLE IMPORTANT PERL PROGRAMS
1. Write a Perl script to convert temperature from centigrade to Fahrenheit.
#!/usr/bin/perl
#temp.pl
print("Enter temperature in centigrade:");
$ce=<STDIN>; #input from the keyboard
$fa=$ce*9/5+32;
print "The temperature $ce in Fahrenheit is $fa\n");
OUTPUT:
$chmod +x temp.pl ; temp.pl
Enter temperature in centigrade: 40.5
The temperature 40.5 in Fahrenheit is 104.9
2. Write a perl script to check whether the entered year is leap year or not using command line arguments?
#leap.pl
die("YOU HAVE NOT ENTERED THE YEAR\n") if(@ARGV==0);
$year=$ARGV[0];
$last=substr($year,-2,2);
if ($last eq "00")
{
$yesorno=($year%400==0?"certainly":"not");
}
else
{
$yesorno=($year%4==0?"certainly":"not");
}
print("$year is".$yesorno."a leap year\n");
OUTPUT:
$leap.pl
YOU HAVE NOT ENTERED THE YEAR
$leap.pl 2004
2004 is certainly a leap year
$leap.pl 1997
1997 is not a leap year
3. Write
a perl script to convert decimal number to binary number
#!/usr/bin/perl
#dec2bin.pl
Foreach $number (@ARGV)
{
$original_no=$number;
Until ($number = =0)
{
$bit=$number%2;
Unshift(@bit_arr,$bit);
$number=int($number/2);
}
$binary_no=join(“ “,@bit_arr);
Print(“The binary number of
$original_no is $binary_no\n”);
$#bit_arr=-1;
}
OUTPUT:
$dec2bin.pl 2 7 65 191 255
The binary number of 2 is 10
The binary number of 7 is 111
The binary number of 65 is 1000001
The binary number of 191 is 10111111
The binary number of 255 is
11111111
No comments:
Post a Comment