SL Lab PDF
SL Lab PDF
SL Lab PDF
ENGINEERING
Aim: Ruby script to create a new string which is n copies of a given string where n is a non-
negative integer
Flowchart:
Ruby Code:
def multiple_string(str, n)
return str*n
end
print multiple_string('a', 1),"\n"
print multiple_string('a', 2),"\n"
print multiple_string('a', 3),"\n"
print multiple_string('a', 4),"\n"
print multiple_string('a', 5),"\n"
Output:
a
aa
aaa
aaaa
aaaaa
2. Write a Ruby script which accept the radius of a circle from the user and compute the parameter
and area.
Aim: Ruby script which accept the radius of a circle from the user and compute the parameter and
area.
Block Structure
Flowchart:
Ruby Code:
radius = 5.0
perimeter = 0.0
area = 0.0
print "Input the radius of the circle: "
radius = gets.to_f
perimeter = 2 * 3.141592653 * radius
area = 3.141592653 * radius * radius
puts "The perimeter is #{perimeter}."
puts "The area is #{area}."
Output:
Aim: Ruby script which accept the user's first and last name and print them in reverse order with
a space between them
Flowchart:
Ruby Code:
Output:
Aim: Ruby script to accept a filename from the user print the extension of that
Flowchart:
Ruby Code:
file = "/user/system/test.rb"
# file name
fbname = File.basename file
puts "File name: "+fbname
# basename
bname = File.basename file,".rb"
puts "Base name: "+bname
# file extention
ffextn = File.extname file
puts "Extention: "+ffextn
# path name
path_name= File.dirname file
puts "Path name: "+path_name
output:
Flowchart:
Ruby Code:
x,y,z = 2,5,4
if x >= y and x >= z
puts "x = #{x} is greatest."
elsif y >= z and y >= x
puts "y = #{y} is greatest."
else
puts "z = #{z} is greatest."
end
output
y = 5 is greatest.
6. Write a Ruby script to print odd numbers from 10 to 1
Flowchart:
Ruby Code:
Output
Aim: Ruby scirpt to check two integers and return true if one of them is 20 otherwise return their
sum
Flowchart:
Ruby Code:
def makes20(x,y)
return x == 20 || y == 20 || x + y == 20
end
Output:
true
false
true
8. Write a Ruby script to check two temperatures and return true if one is less than
0 andthe other is greater than 100
Aim: Ruby script to check two temperatures and return true if one is less than 0 and the other is
greater than 100
Flowchart:
Ruby Code:
Output
true
true
false
9. Write a Ruby script to print the elements of a given array
Block Structure
Flowchart:
Ruby Code:
Output
Ruby
2.3
2017-12-28 06:01:53 +0000
10 Write a Ruby program to retrieve the total marks where subject name and marks of astudent stored in a
hash
Aim: Ruby program to retrieve the total marks where subject name and marks of a student storedin a hash
Flowchart
Ruby Code:
Output
Source Code:
set fact 1
for {set i 0} {$i <= 10} {incr i} {
puts "$i! = $fact"
set fact [expr {$fact * ($i + 1)}]
}
Output:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
12) Write a TCL script that multiplies the numbers from 1 to 10
Source Code:
Output:
13) Write a TCL script for Sorting a list using a comparison function
Source Code:
set mylist { 7 3 5 2 }
compare $mylist
Output:
2357
14 Write a TCL script to (i)create a list (ii) append elements to the list (iii)Traverse the list
(iv)Concatenate the list
Create a list
Syntax:
Source code:
#!/usr/bin/tclsh
Output:
Syntax:
Source Code:
#!/usr/bin/tclsh
Output:
Source Code:
set L1 {1 2 3 }
puts $L1
lappend L1 4 5
puts "After append $L1"
set L2 {-1 0}
puts "List 2 $L2"
Output:
Output:
123
After append 1 2 3 4 5
Traversing list
1
2
3
4
5
List 2 -1 0
After concat -1 0 1 2 3 4 5
15) Write a TCL script to comparing the file modified times.
Source Code:
Output:
16) Write a TCL script to Copy a file and translate to native format.
Source Code:
Output:
17) a) Write a Perl script to find the largest number among three numbers.
b) Write a Perl script to print the multiplication tables from 1-10 using subroutines.
a) Write a Perl script to find the largest number among three numbers.
Source Code:
#!/usr/bin/perl
print "Enter a number\n";
chomp($a=<stdin>);
print "Enter second number\n";
chomp($b=<stdin>);
print "Enter third number\n";
chomp($c=<stdin>);
$big=0;
$equal=0;
if($a eq $b){
$big = $a;
$equal = $a;
}
elsif($a > $b){
$big=$a;
}
else{
$big = $b;
}
if($equal eq $c){
print "All numbers are same";
}
elsif($big < $c){
$big = $c;
}
else{
print "The biggest number is $big \n";
}
Output:
Enter a number
789
Enter second number
258
Enter third number
254
The biggest number is 789
Source Code:
sub table{
my $i = 1;
my $loop;
foreach $loop(@_){
for($i;$i<=10;$i++){
my $ans = $i*$loop;
print"$loop*$i=$ans \n";
}
print"\n";
}
}
Output:
multiplication tables from 1-10 using subroutines
1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1*10=10
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
6*1=6
6*2=12
6*3=18
6*4=24
6*5=30
6*6=36
6*7=42
6*8=48
6*9=54
6*10=60
7*1=7
7*2=14
7*3=21
7*4=28
7*5=35
7*6=42
7*7=49
7*8=56
7*9=63
7*10=70
8*1=8
8*2=16
8*3=24
8*4=32
8*5=40
8*6=48
8*7=56
8*8=64
8*9=72
8*10=80
9*1=9
9*2=18
9*3=27
9*4=36
9*5=45
9*6=54
9*7=63
9*8=72
9*9=81
9*10=90
10*1=10
10*2=20
10*3=30
10*4=40
10*5=50
10*6=60
10*7=70
10*8=80
10*9=90
10*10=100
18. Write a Perl program to implement the following list of manipulating functions
a)Shift
b) Unshift
c) Push
a) shiift
shift function
This function returns the first value in an array, removing it and shifting the elements of the array list to the left by
one. Shift operation removes the value like pop but is taken from the start of the array instead of the end as in pop.
This function returns undef if the array is empty otherwise returns first element of the array.
Syntax: shift(Array)
Source Code:
#!/usr/bin/perl
Output:
b) Unshift
unshift function
This function places the given list of elements at the beginning of an array. Thereby shifting all the values in an
array by right. Multiple values can be unshift using this operation. This function returns the number of new
elements in an array.
Source Code:
#!/usr/bin/perl
output:
push function
This function inserts the values given in the list at an end of an array. Multiple values can be inserted separated
by comma. This function increases the size of an array. It returns number of elements in new array.
Source Code:
#!/usr/bin/perl
output:
substitution Operator or ‘s’ operator in Perl is used to substitute a text of the string with some pattern
specified by the user.
Syntax: s/text/pattern
Returns: 0 on failure and number of substitutions on success
Source Code:
#!/usr/bin/perl -w
output:
GeekstoGeeks
b) Write a Perl script to validate IP address and email address.
Source Code:
#!/usr/bin/perl
if($ip =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
{
$ip = $1;
print "$ip\n";
}
Output:
20) Write a Perl script to print the file in reverse order using command line arguments
Source code:
#!/usr/bin/perl -w
output:
dlroW olleH
skeeG roF skeeG