Perl

Perl combines the power of some of most powerful UNIX tools. It is standard on linux and also offered on solaris 8. However, it is free and executable are available for all unix system.



1. Perl Program to input a number and store it in a variable. Make a friend guess the number. Have your program tell your friend whether their guess was high, low, or correct.


#! /usr/bin/perl -w
print "Enter any number :";
my $ip1 = <STDIN>;
print "Guess any number:";
my $ip2 = <STDIN>;
if($ip2 > $ip1)
 {print "ip2 is greater than ip1 ";}
elsif($ip2 < $ip1)
 {print "ip2 is smaller than ip1 ";}
else
 {print "ip2 is equal to ip1 ";}


2. Write a program that stores a number and keeps trying to get user input until the user enters the number correctly. As soon as the correct number is entered, it prints: Correct!


#! /usr/bin/perl -w

my $x=16;
my $y;
do{
 print "Enter number:";
    $y=<STDIN>;
 }while($x!=$y);



3. Write a PERL program that reads in two numbers and does the following: a. It prints Error: can't divide by zero if the second number is 0. b. If the first number is 0 or the second number is 1, it just prints the first number (because no division is necessary). c. In all other cases, it divides the first number by the second number and prints the result.


#! /usr/bin/perl -w

print "Enter first number :";
$ip1 = <STDIN>;
print "Enter second number:";
my $ip2 = <STDIN>;
if($ip2 == 0)
 {print "cant divide no. by 0 ";}
elsif($ip2 == 1 || $ip1 ==0)
 {print "$ip1";}
else
 {
  my $x = $ip1/$ip2;
  print "ans = $x";
  } 
print "\n"




Comments

Popular posts from this blog

Python Code

C++ code