1. Create, test and debug a Ruby program called dognames.rb or catnames.rb to accept 3 names from the keyboard and to display each name on the screen in alphabetical order WITHOUT using a data structure such as a list.
This is my first Ruby program dognames.rb. I use Ruby interpreter (irb.bat) for test and debug the Ruby program. And then run the program from the console window using either C:\ruby dogname.rb.
def dognames
puts "Whats your dog name: "
$dog1 = gets
puts "Whats your dog name: "
$dog2 = gets
puts "Whats your dog name: "
$dog3 = gets
array = [$dog1,$dog2,$dog3]
puts "your dog name"
puts array.sort
dognames
puts "your dog name"
puts array.sort
dognames
2. Write a Ruby program called fizzbuzz.rb that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz“. For numbers which are multiples of both three and five print “FizzBuzz“.
This is my Fizzbuzz program fizzbuzz.rb.
def FizzBuzz
1.upto(100) do |i|
if i % 5 == 0 and i % 3 == 0
puts "FizzBuzz"
elsif i % 5 == 0
puts "Buzz"
elsif i % 3 == 0
puts "Fizz"
else
puts i
end
end
end
fizzbuzz
No comments:
Post a Comment