0% found this document useful (0 votes)
33 views4 pages

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) "

Uploaded by

Bhargav NanIE
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)
33 views4 pages

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) "

Uploaded by

Bhargav NanIE
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/ 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