0% found this document useful (0 votes)
14 views

SL LAB Mid

Uploaded by

Sahil Venkat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

SL LAB Mid

Uploaded by

Sahil Venkat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DEPARTMENT OF CSE @ MRIET CSE(AIML) –SCRIPTING LANGUAGES LAB (21CA623PE) DEPARTMENT OF CSE @ MRIET CSE(AIML) –SCRIPTING LANGUAGES LAB

CSE(AIML) –SCRIPTING LANGUAGES LAB (21CA623PE) DEPARTMENT OF CSE @ MRIET CSE(AIML) –SCRIPTING LANGUAGES LAB (21CA623PE)

1. Write a Ruby script to create a new string which is n copies of a given string where n is a non- 2. Write a Ruby script which accept the radius of a circle from the user and compute the parameter 3. Write a Ruby script which accept the user's first and last name and print them in
negative integer and area. reverse orderwith a space between them
Aim: Ruby script to create a new string which is n copies of a given string where n is a non- Aim: Ruby script which accept the radius of a circle from the user and compute the parameter and Aim: Ruby script which accept the user's first and last name and print them in reverse order with
negative integer area. a space between them

Block Structure Flowchart:


Flowchart:

Flowchart:

Ruby Code:
def multiple_string(str, n)
return str*n
end
print multiple_string('a', 1),"\n"
print multiple_string('a', 2),"\n" Ruby Code:
print multiple_string('a', 3),"\n"
print multiple_string('a', 4),"\n" puts "Input your first name: "
print multiple_string('a', 5),"\n" fname = gets.chomp
puts "Input your last name: "
lname = gets.chomp
Ruby Code:
puts "Hello #{lname} #{fname}"
radius = 5.0
Output: perimeter = 0.0
a area = 0.0
aa print "Input the radius of the circle: "
aaa radius = gets.to_f Output:
aaaa perimeter = 2 * 3.141592653 * radius
aaaaa area = 3.141592653 * radius * radius Input your first name:
puts "The perimeter is #{perimeter}." Input your last name:
puts "The area is #{area}." Hello Naga babu

Output:

Input the radius of the circle: 2


The perimeter is 12.566370612.
The area is 12.566370612.

DEPARTMENT OF CSE @ MRIET CSE(AIML) –SCRIPTING LANGUAGES LAB (21CA623PE) DEPARTMENT OF CSE @ MRIET CSE(AIML) –SCRIPTING LANGUAGES LAB (21CA623PE) DEPARTMENT OF CSE @ MRIET CSE(AIML) –SCRIPTING LANGUAGES LAB (21CA623PE)

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
4. Write a Ruby script to accept a filename from the user print the extension of that
Aim: Ruby script to find the greatest of three numbers

Flowchart: Aim: Ruby script to print odd numbers from 10 to 1


Aim: Ruby script to accept a filename from the user print the extension of that
Flowchart:

Flowchart:

Ruby Code:

file = "/user/system/test.rb"
# file name Ruby Code:
fbname = File.basename file
puts "File name: "+fbname x,y,z = 2,5,4
# basename if x >= y and x >= z
bname = File.basename file,".rb" puts "x = #{x} is greatest."
puts "Base name: "+bname elsif y >= z and y >= x
# file extention puts "y = #{y} is greatest." Ruby Code:
ffextn = File.extname file else
puts "Extention: "+ffextn puts "z = #{z} is greatest." puts "Odd numbers between 9 to 1: "
# path name end 9.step 1, -2 do |x|
path_name= File.dirname file puts "#{x}"
puts "Path name: "+path_name end

output Output
output:
y = 5 is greatest. Odd numbers between 9 to 1:
File name: test.rb 9
Base name: test 7
Extention: .rb 5
Path name: /user/system 3
1
DEPARTMENT OF CSE @ MRIET CSE(AIML) –SCRIPTING LANGUAGES LAB (21CA623PE) DEPARTMENT OF CSE @ MRIET CSE(AIML) –SCRIPTING LANGUAGES LAB (21CA623PE) DEPARTMENT OF CSE @ MRIET CSE(AIML) –SCRIPTING LANGUAGES LAB (21CA623PE)

7. Write a Ruby scirpt to check two integers and return true if one of them is 20 8. Write a Ruby script to check two temperatures and return true if one is less than 9. Write a Ruby script to print the elements of a given array
otherwisereturn their sum 0 andthe other is greater than 100
Aim: Ruby script to print the elements of a given array
Aim: Ruby scirpt to check two integers and return true if one of them is 20 otherwise return their Aim: Ruby script to check two temperatures and return true if one is less than 0 and the other is
sum greater than 100 Block Structure

Flowchart:
Flowchart:

Flowchart:
Ruby Code:

def makes20(x,y)
return x == 20 || y == 20 || x + y == 20
end Ruby Code:

print makes20(10, 10),"\n" def temp(temp1, temp2)


print makes20(40, 10),"\n" return ( temp1 < 0 && temp2 > 100 ) || ( temp1 > 100 && temp2 < 0 );
print makes20(15, 20) end
print temp(110, -1),"\n"
print temp(-1, 110),"\n"
print temp(2, 120)
Ruby Code:

array1 = ["Ruby", 2.3, Time.now]


Output: for array_element in array1
true Output puts array_element
false end
true true
true
false
Output

Ruby
2.3
2017-12-28 06:01:53 +0000

DEPARTMENT OF CSE @ MRIET CSE(AIML) –SCRIPTING LANGUAGES LAB (21CA623PE)

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:

student_marks = Hash.new 0 student_marks['Literature'] = 50


student_marks['Science'] = 40
student_marks['Math'] = 90
total_marks = 0 student_marks.each {|key,value|
total_marks +=value
}
puts "Total Marks: "+total_marks.to_s

Output

Total Marks: 180

You might also like