You are on page 1of 4

Roll No:17A81A0570

Write Ruby program reads a number and calculates the factorial value of it and prints the
same.

Aim:To develop an ruby program reads a number and calculates the factorial value and print it.

Source code:

puts "Enter a number"


n = gets.to_i
f=1
i=1
while i<=n do
f = f*i
i= i+1
end
puts "factorial of #{n} is #{f}"

Output:
Roll No:17A81A0570

Write a Ruby program which counts number of lines in a text file using its regular expressions
facility.

Aim:To develop a ruby program which counts number of lines in a text file using its regular
expressions facility.

Source code:

print "Enter file name:";


fname = gets.chomp;
aFile = File.new(fname, "r")
if aFile
puts "file exists"
arr = IO.readlines(fname)
#using each
arr.each do |i|
puts i
end
# using for
for m in arr
puts m
end
count = IO.readlines(fname).size
puts "There are #{count} lines in #{fname}";
else
puts "Unable to open file!"
end

Output:
Roll No:17A81A0570

Write a Ruby program that uses iterator to find out the length of a string.

Aim:To develop a ruby program that uses iterator to find out the length of a string.

Source code:

print "Enter a string:"


str = gets.chomp
c=0
str.each_char do |i|
c=c+1
end
puts "Length of #{str}: #{c}"

output:
Roll No:17A81A0570

You might also like