List of Experiments:
1. Write a Ruby script to create a new string which is n copies of a given string where n is a non-negative integer.
2. Write a Ruby script which accept the radius of a circle from the user and compute the perimeter and area.
3. Write a Ruby script which accept the users first and last name and print them in reverse order with a space
between them.
4. Write a Ruby script to accept a filename from the user print the extension of that.
5. Write a Ruby script to find the greatest of three numbers.
6. Write a Ruby script to print odd numbers from 10 to 1.
7. Write a Ruby script to check two integers and return true if one of them is 20 otherwise return their sum.
8. Write a Ruby script to check two temperatures and return true if one is less than 0 and the other is greater than
100.
9. Write a Ruby script to print the elements of a given array
10. Write a Ruby program to retrieve the total marks where subject name and marks of a student stored in a hash
11. Write a TCL script to find the factorial of a number.
12. Write a TCL script that multiplies the numbers from 1 to 10.
13. Write a TCL script for sorting a list using a comparison function.
14. Write a TCL script to
(i) create a list
(ii) append elements to the list
(iii) Traverse the list
(iv) Concatenate the list
15. Write a TCL script to comparing the file modified times
16. Write a TCL script to Copy a file and translate to native format.
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.
18. Write a Perl program to implement the following list of manipulating functions
a) Shift
b) Unshift
c) Push
19. a) Write a Perl script to substitute a word, with another word in a string. b) Write a Perl script to validate IP
address and email address.
20. Write a Perl script to print the file in reverse order using command line arguments.
[Link] a Ruby program to create a new string which is n copies of a given string where n is a
non-negative integer.
Solution :
PROGRAM: (str_n_copies.rb)
def string_copies(str, n)
return str * n # returns n copies of str
end
print "Enter a string: "
input_str = [Link]
print "Enter a non-negative integer: "
n = gets.to_i
result = string_copies(input_str, n)
puts "Result: #{result}"
OUTPUT:
Enter a string: StudyGlance
Enter a non-negative integer: 4
Result: StudyGlanceStudyGlanceStudyGlanceStudyGlance
2. Write a Ruby program which accept the radius of a circle from the user and compute the
perimeter and area.
Solution :
PROGRAM: (perimeter_area.rb)
print "Enter the radius of the circle: "
radius = gets.to_f
perimeter = 2 * Math::PI * radius
area = Math::PI * radius**2
puts "Perimeter (Circumference): #{[Link](4)}"
puts "Area: #{[Link](4)}"
OUTPUT:
Enter the radius of the circle: 5.0
Perimeter : 31.4
Area: 78.5
3. Write a Ruby program which accept the user's first and last name and print them in reverse
order with a space between them.
Solution :
PROGRAM: (reverse_names.rb)
print "Enter your first name: "
first_name = [Link]
print "Enter your last name: "
last_name = [Link]
puts "Hello #{last_name} #{first_name}"
OUTPUT:
Enter your first name: Glance
Enter your last name: Study
Hello Study Glance
4. Write a Ruby program to accept a filename from the user print the extension of that.
Solution :
PROGRAM: (file_extension.rb)
print "Enter a filename: "
f_name = [Link]
extension = [Link](f_name)
puts "File extension: #{extension}"
OUTPUT:
Enter a filename: [Link]
File extension: .py
Aim:
☛ Write a Ruby program to find the greatest of three numbers.
Solution :
PROGRAM: (greatest_num.rb)
puts "Enter three numbers:"
a = gets.to_i
b = gets.to_i
c = gets.to_i
if a >= b && a >= c
puts "#{a} is the greatest."
elsif b >= a && b >= c
puts "#{b} is the greatest."
else
puts "#{c} is the greatest."
end
OUTPUT:
Enter three numbers:
45
25
21
45 is the greatest.
[Link] a Ruby program to find the greatest of three numbers.
Solution :
PROGRAM: (greatest_num.rb)
puts "Enter three numbers:"
a = gets.to_i
b = gets.to_i
c = gets.to_i
if a >= b && a >= c
puts "#{a} is the greatest."
elsif b >= a && b >= c
puts "#{b} is the greatest."
else
puts "#{c} is the greatest."
end
OUTPUT:
Enter three numbers:
45
25
21
45 is the greatest.
6. Write a Ruby program to print odd numbers from 10 to 1.
Solution :
PROGRAM: (odd_nums.rb)
num = 10
while num >= 1
if num % 2 != 0
puts num
end
num -= 1
end
OUTPUT:
9
7
5
3
1
Aim:
7. Write a Ruby program to check two integers and return true if one of them is 20 otherwise
return their sum.
Solution :
PROGRAM: (check_num.rb)
def check_integers(a, b)
if a == 20 || b == 20
true
else
a + b
end
end
puts "Enter first number:"
x = gets.to_i
puts "Enter second number:"
y = gets.to_i
result = check_integers(x, y)
puts "Result: #{result}"
OUTPUT:
# Sample Run 1:
--------------
Enter first number:
15
Enter second number:
20
Result: true
# Sample Run 2:
---------------
Enter first number:
14
Enter second number:
15
Result: 29
8. Write a Ruby program to check two temperatures and return true if one is less than 0 and the
other is greater than 100.
Solution :
PROGRAM: (check_temperatures.rb)
def check_temps(temp1, temp2)
return (temp1 < 0 && temp2 > 100) || (temp2 < 0 && temp1 > 100)
end
puts "Enter first temperature:"
temp1 = gets.to_i
puts "Enter second temperature:"
temp2 = gets.to_i
result = check_temps(temp1, temp2)
puts "Result: #{result}"
OUTPUT:
# Sample Run 1:
--------------
Enter first temperature:
110
Enter second temperature:
-2
Result: true
# Sample Run 2:
---------------
Enter first temperature:
-5
Enter second temperature:
120
Result: true
# Sample Run 3:
---------------
Enter first temperature:
34
Enter second temperature:
125
Result: false
# Sample Run 4:
---------------
Enter first temperature:
-10
Enter second temperature:
85
Result: false
9. Write a Ruby program to print the elements of a given array.
Solution :
PROGRAM: (array_elements.rb)
ip_array = [10, 20, 30, 40, 50]
for element in ip_array
puts element
end
OUTPUT:
10
20
30
40
50
10. Write a Ruby program to retrieve the total marks where subject name and marks of a student
stored in a hash.
Solution :
PROGRAM: (total_marks.rb)
student_marks = {
"Maths" => 85,
"Science" => 92,
"English" => 78,
"Hindi" => 88,
"Telegu"=> 90
}
total_marks = 0
student_marks.each do |subject, marks|
total_marks += marks
end
puts "Total Marks: #{total_marks}"
OUTPUT:
Total Marks: 433
11. Write a TCL script to find the factorial of a number?
Solution :
PROGRAM: ([Link])
puts "Enter a number:"
set num [gets stdin]
set fact 1
for {set i 1} {$i <= $num} {incr i} {
set fact [expr {$fact * $i}]
}
puts "Factorial of $num is $fact"
puts "Total Marks: #{total_marks}"
OUTPUT:
Enter a number:
5
Factorial of 5 is 120
(OR)
PROGRAM: (factorial_recur.tcl)
proc factorial {n} {
if {$n <= 1} {
return 1
} else {
return [expr {$n * [factorial [expr {$n - 1}]]}]
}
}
puts "Enter a number:"
set num [gets stdin]
puts "Factorial of $num is [factorial $num]"
OUTPUT:
Enter a number:
5
Factorial of 5 is 120
12. Write a TCL script that multiplies the numbers from 1 to 10?
Solution :
PROGRAM: (multiplies_nums.tcl)
proc multiplyNums {} {
set product 1
for {set i 1} {$i <= 10} {incr i} {
set product [expr {$product * $i}]
}
return $product
}
# Call the procedure
set result [multiplyNums]
puts "The product of numbers from 1 to 10 is: $result"
OUTPUT:
The product of numbers from 1 to 10 is: 3628800
[Link] a TCL script for Sorting a list using a comparison function
Solution :
PROGRAM: (sort_list.tcl)
proc compare {mylist} {
set len [llength $mylist]
set len [expr {$len - 1}]
for {set i 0} {$i < $len} {incr i} {
for {set j 0} {$j < [expr {$len - $i}]} {incr j} {
set a [lindex $mylist $j]
set b [lindex $mylist [expr {$j + 1}]]
if {$a > $b} {
# swap elements
set temp $a
lset mylist $j $b
lset mylist [expr {$j + 1}] $temp
}
}
}
puts $mylist
}
set mylist {17 31 15 22}
compare $mylist
OUTPUT:
15 17 22 31
[Link] a TCL script to
(i)create a list
(ii)append elements to the list
(iii)Traverse the list
(iv)Concatenate the list
Solution :
PROGRAM: (list_ops.tcl)
# (i) Create a list
set mylist1 {10 20 30}
puts "Original List: $mylist1"
# (ii) Append elements to the list
lappend mylist1 40
lappend mylist1 50
puts "After Appending Elements: $mylist1"
# (iii) Traverse the list
puts "Traversing the List:"
foreach item $mylist1 {
puts $item
}
# (iv) Concatenate the list with another list
set myList2 {60 70 80}
set newList [concat $mylist1 $myList2]
puts "Concatenated List: $newList"
OUTPUT:
Original List: 10 20 30
After Appending Elements: 10 20 30 40 50
Traversing the List:
10
20
30
40
50
Concatenated List: 10 20 30 40 50 60 70 80
[Link] a TCL script to comparing the file modified times.
Solution :
PROGRAM: (compare_files.tcl)
set file1 "[Link]"
set file2 "[Link]"
set t1 [file mtime $file1]
set t2 [file mtime $file2]
if {$t1 > $t2} {
puts "$file1 is newer"
} elseif {$t1 < $t2} {
puts "$file2 is newer"
} else {
puts "Both files have same modified time"
}
OUTPUT:
[Link] is newer
16. Write a TCL script to comparing the file modified times.
Solution :
PROGRAM: (compare_files.tcl)
set file1 "[Link]"
set file2 "[Link]"
set t1 [file mtime $file1]
set t2 [file mtime $file2]
if {$t1 > $t2} {
puts "$file1 is newer"
} elseif {$t1 < $t2} {
puts "$file2 is newer"
} else {
puts "Both files have same modified time"
}
OUTPUT:
[Link] is newer
17)a) Write a Perl script to find the largest number among three numbers.
Solution :
PROGRAM: (largest_num.pl)
# read 3 numbers from user
print "enter x value : ";
$x=<stdin>;
print "enter y value : ";
$y=<stdin>;
print "enter z value : ";
$z=<stdin>;
# Find largest
my $largest = $x;
if ($y > $largest) {
$largest = $y;
}
if ($z > $largest) {
$largest = $z;
}
print "The largest number is: $largest\n";
OUTPUT:
enter x value : 12
enter y value : 42
enter z value : 8
The largest number is: 42
b) Write a Perl script to print the multiplication tables from 1-10 using subroutines.
Solution :
PROGRAM: (tables_print.pl)
#!/usr/bin/perl
use strict;
use warnings;
# Subroutine to print table of a number
sub print_table {
my ($num) = @_;
for my $i (1..10) {
print "$num x $i = ", $num * $i, "\n";
}
print "\n";
}
# Print tables from 1 to 10
for my $n (1..10) {
print "Multiplication Table for $n:\n";
print_table($n);
}
OUTPUT:
Multiplication Table for 1:
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
Multiplication Table for 2:
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Multiplication Table for 3:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Multiplication Table for 4:
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
Multiplication Table for 5:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Multiplication Table for 6:
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
Multiplication Table for 7:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Multiplication Table for 8:
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
Multiplication Table for 9:
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
Multiplication Table for 10:
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100
18. Write a Perl program to implement the following list of manipulating functions
a) Shift
b) Unshift
c) Push.
Solution :
PROGRAM: (list_functions.pl)
#!/usr/bin/perl
use strict;
use warnings;
# Initial list
my @numbers = (10, 20, 30, 40);
print "Original list: @numbers\n";
# a) Shift - removes first element
my $shifted = shift @numbers;
print "After shift (removed $shifted): @numbers\n";
# b) Unshift - adds element at beginning
unshift @numbers, 5;
print "After unshift (added 5 at beginning): @numbers\n";
# c) Push - adds element at end
push @numbers, 50;
print "After push (added 50 at end): @numbers\n";
OUTPUT:
Original list: 10 20 30 40
After shift (removed 10): 20 30 40
After unshift (added 5 at beginning): 5 20 30 40
After push (added 50 at end): 5 20 30 40 50
19) a. Write a Perl script to substitute a word, with another word in a string.
Solution :
PROGRAM: (replace_str.pl)
$sentence = "Perl is great at manipulating strings, naturally.";
print "$sentence \n\n";
$replacement = "is totally awesome";
substr($sentence, 5, 8) = $replacement;
print "Substituting $replacement staring at 5 and going 8 words \n";
print "$sentence \n\n";
OUTPUT:
Perl is great at manipulating strings, naturally.
Substituting is totally awesome staring at 5 and going 8 words
Perl is totally awesome at manipulating strings, naturally.
b) Write a Perl script to validate IP address and email address.
Solution :
PROGRAM: (validate_ipaddr.pl)
#!/usr/bin/perl
use strict;
use warnings;
print "Enter an IP address: ";
chomp(my $ip = <STDIN>);
print "Enter an Email address: ";
chomp(my $email = <STDIN>);
# --------------------------
# Validate IP Address
# --------------------------
# Pattern checks 0�255 for each octet
my $ip_pattern = qr/^
(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d) \.
(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d) \.
(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d) \.
(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)
$/x;
if ($ip =~ $ip_pattern) {
print "Valid IP address\n";
} else {
print "Invalid IP address\n";
}
# --------------------------
# Validate Email Address
# --------------------------
my $email_pattern = qr/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
if ($email =~ $email_pattern) {
print "Valid Email address\n";
} else {
print "Invalid Email address\n";
}
OUTPUT:
# Sample Run 1:
Enter an IP address: [Link]
Enter an Email address: madhu@[Link]
Valid IP address
Valid Email address
# Sample Run 2:
Enter an IP address: 158.23.1
Enter an Email address: abc@[Link]
Invalid IP address
Valid Email address
# Sample Run 3:
Enter an IP address: [Link]
Enter an Email address: madhu@
Valid IP address
Invalid Email address
20. Write a Perl script to print the file in reverse order using command line arguments
Solution :
INPUT FILE: "[Link]"
1
2
3
4
5
PROGRAM: ([Link])
#!/usr/bin/perl
use strict;
use warnings;
# Check if filename is passed
if (@ARGV != 1) {
die "Usage: perl [Link] <filename>\n";
}
my $file = $ARGV[0];
# Open the file
open(my $fh, "<", $file) or die "Cannot open file '$file': $!";
# Read all lines into an array
my @lines = <$fh>;
close($fh);
# Print in reverse order
print reverse @lines;
OUTPUT:
# perl [Link] [Link]
5
4
3
2
1