Você está na página 1de 23

MCE Societys

Allana Institute of Management and Sciences, Pune


MCA III (B) Division
LAB Assignment 1
PERL
(SHAIKH ANIS-64)

ASSIGNMENT-1
Addition of two numbers

print "Enter 1st Value:";


$a=<>;
print "Enter 2nd Value";
$b=<>;
$c=$a+$b;
print "a=".$a."\n b=".$b."\n Addintion Is:".$c;
<STDIN>
OUTPUT:

ASSIGNMENT-2

WAP to swap values of two variables

print "Enter 1st Value:";


$a=<>;
print "Enter 2nd Value:";
$b=<>;
print "Before Swap a=$a and b=$b";
$temp=$a;
$a=$b;
$b=$temp;
print "After Swap a=$a and b=$b";
<STDIN>
OUTPUT:

ASSIGNMENT-3

WAP to print 12+22+32----------------- take last value from user

print "Enter The Last Value:";


$a=<>;
$sum=0;
for($i=2;$i<=$a;$i++)
{
$i=$i+10;
if($i<=$a)
{
$sum=$sum+$i;
print "$i +";
$i--;
}
}
print "\n Sum=$sum";
<STDIN>

OUTPU

ASSIGNMENT-4
WAP to find out maximum and minimum no. from array, take input from user

print "Enter the no of terms:";


$a=<>;
for($i=0;$i<$a;$i++)
{
$b=<>;
push(@array,$b);
}
print "@array";
for (@array)

{
$min = $_ if !$min || $_ < $min;
$max = $_ if !$max || $_ > $max;
}
print "min: $min\n";
print "max: $max\n";
<STDIN>

OUTPUT:

ASSIGNMENT-5
WAP to calculate average, mean, median, mode, take input from user

print "Enter The Terms: ";


$a=<>;
$sum=0;
for($i=0;$i<$a;$i++)
{
$b=<>;
push(@array,$b);
$sum=$sum+$b;
}

print "@array";
$mean=$sum/$a;
print "Mean is: $mean\n";
for($i=0;$i<$a-1;$i++)
{
for($j=$i+1;$j<$a;$j++)
{
if(@array[$i]>@array[$j])
{
$temp=@array[$i];
@array[$i]=@array[$j];
@array[$j]=$temp;
}
}
}
print "@array";
$l=$a/2;
$m=$a-1;
if($a%2==0)
{
$med=(@array[$l]+@array[$m/2])/2;
}
else
{
$med=@array[$l];
}

print "Median Is: $med\n";


$max=0;
for($i=0;$i<$a-1;$i++)
{
$mode=0;
for($j=$i+1;$j<$a;$j++)
{
if(@array[$i]==@array[$j])
{
$mode++;
}
}
if(($mode>$max)&&(mode!=0))
{
$k=0;
$max=$mode;
@b[$k]=@array[$i];
$k++;
}
else
{
if($mode==$max)
{
@b[$k]=@array[$i];
$k++;
}

}
}
$c=0;
for($i=0;$i<$a;$i++)
{
if(@array[$i]==@b[$i])
{
$c++;
}
}
if($c==$a)
{
print "\nThere is no mode";
}
else
{
print "Mode is: "
for($i=0;$i<$k;$i++)
{
print "@b[$i]";
}
}
<STDIN>
OUTPUT:

ASSIGNMENT-6
WAP take input from user count no. of character, spaces, words
print "Enter The file Name:";
$a=<>;
open(FILE,$a)||diw("Cannot open file!");
$total_nlines = 0;
$total_nwords = 0;
$total_nletters = 0;
while ($line = <FILE>)
{
@words = split(" ",$line);
$nwords = @words;

for ($i = 0; $i < $nwords; $i = $i + 1)


{
@letters = split("",$words[$i]);
$nletters = @letters;
$total_nletters = $total_nletters + $nletters;
}
$total_nwords = $total_nwords + $nwords;
$total_nlines = $total_nlines + 1;
}
print "$filename contains $total_nlines lines, $total_nwords words and
$total_nletters letters.\n";
close(FILE);
<STDIN>

OUTPUT:

ASSIGNMENT-8
Write the subroutine that takes the string as argument and return the string in reverse order by
word.
print "Enter The String To Reverse:";
@rev=fun($a);
print "\nAfter revese: @rev";
sub fun
{
$s=<>;
@array=split(undef,$s);
$cnt=0;
foreach(@array)
{

$cnt++;
}
for($i=0,%j=$cnt-1;$i<$cnt/2;$i++,$j--)
{
$temp=$array[$i];
$array[$i]=$array[$j];
$array[$j]=$temp;
}
return @array;
}
<STDIN>

OUTPUT:

ASSIGNMENT-9
Write a script that copies content of one file into another
print "Enter 1st File Name:";
$a=<>;
open(DATA1,"<$a")||diw("Cannot open file!");
print "Enter 2nd File Name to Write: ";
$b=<>;
open(DATA2, ">$b")||diw("Cannot open file!");
while(<DATA1>)
{
print DATA2 $_;
}
print "Aftre Copying: ";

close( DATA1 );
close( DATA2 );
<STDIN>

OUTPUT:

ASSIGNMENT-10
Write a script for the file which having some data. Find out the computer word from that data
and print how many times this word is occurred. */

print "Enter The file Name:";


$a=<>;
open(FILE,$a)||diw("Cannot open file!");
print "Wht YOu Want to Find In File: ";
$b=<>;
$cnt=0;
$i=0;
while (<FILE>)
{
@array=split(undef,$_);

foreach(@array)
{
if(@array[$i] eq '$b')
{
$cnt++;
}
}
}
print "\n$b ocurance in file: $cnt";
close(FILE);
<STDIN>

OUTPUT:

ASSIGNMENT-11
Write program to accept file name from command line. The file contains line of text where each
line is persons name. Convert all the letters of names in uppercase & display sorted list

print "Enter 1st File Name:";


$a=<>;
open(DATA1,"<$a")||diw("Cannot open file!");
open(DATA2, ">zz.txt")||diw("Cannot open file!");
while(<DATA1>)
{
@c=uc $_;
print DATA2 @c;
}
print "Converted in Uppercase: ";

close( DATA1 );
close( DATA2 );
<STDIN>

OUTPUT:

ASSIGNMENT-12
WAP to test function that computes the median of a given array. Even & odd array must be
check. If the array length is odd middle element of sorted array as median else for even length
array the median is the average of two middle element of sorted array.

print "Enter The Terms: ";


$a=<>;
for($i=0;$i<$a;$i++)
{
$b=<>;
push(@array,$b);
}
$b=test(@array);
print "Median Is: $b";
sub test

{
for($i=0;$i<$a-1;$i++)
{
for($j=$i+1;$j<$a;$j++)
{
if(@array[$i]>@array[$j])
{
$temp=@array[$i];
@array[$i]=@array[$j];
@array[$j]=$temp;
}
}
}
$cnt=0;
foreach(@array)
{
$cnt++;
}
print "@array";
$l=$cnt/2;
$m=$cnt-1;
if($cnt%2==0)
{
$med=(@array[$l]+@array[$m/2])/2;
}
else

{
$med=@array[$l];
}
return $med;
}
<STDIN>

OUTPUT:

Você também pode gostar