Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions add_up.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# (3) Write a program with a function add_up(i) .
# It is to be passed a positive integer, and it will add all the numbers from 1 to that integer and return the sum.
# Call the function three times within the program, and each time print out the return value. Call the program add_up.rb.

def add_up(num)
sum = num
while num > 0
# p "Before we substract: #{num}"
num = num - 1
sum += num
# p "After we subtract: #{num}"
# p sum
end
return sum
end
puts add_up(5)
puts add_up(2)
puts add_up(6)
16 changes: 16 additions & 0 deletions full_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# (1) Write a program which asks for a person's first name, then middle, then last.
# It should store each of these parts in an array. Finally, it should greet the person using their full name.
# Call the program full_name.rb.

full_name = []
puts "What is your first name:?"
first_name = gets.chomp
puts "What is your middle name?"
middle_name = gets.chomp
puts "What is your last name?"
last_name = gets.chomp
full_name << gets

puts 'Hello ' + first_name + ' ' + middle_name + ' ' + last_name


19 changes: 19 additions & 0 deletions leap_year.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# (4) Write a program, leap_year.rb.
# It will ask the user for a starting year and an ending year, and it will then print out all the leap years between them,
# including the starting or ending year if those are leap years. The rules for leap years are:
# A leap year is divisible by 4, except for years that are divisible by 100 --
# those aren't leap years -- except for years that are divisible by 400, which ARE leap years.

puts 'Give a starting year'
start_year = gets.chomp.to_i
puts 'Give an ending year'
end_year = gets.chomp.to_i
puts ''

while start_year.to_i <= end_year.to_i
if start_year.to_i % 4 == 0
puts start_year
end

start_year = start_year.to_i + 1
end
13 changes: 13 additions & 0 deletions sorted_words.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# (2) Write a program called sorted_words.rb.
# It should prompt the user for words and add each to an array.
# The user should be able to add as many words as they like, until they just hit enter to return a blank word.
# Then sort the array using the sort method and print it out.

sorted_words = []
puts "Enter random words:\n(Hit enter to exit)"
while (entry = gets.chomp)
break if entry.empty?
sorted_words.push entry
end

puts sorted_words.sort