diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..e85020c Binary files /dev/null and b/.DS_Store differ diff --git a/add_up.rb b/add_up.rb new file mode 100644 index 0000000..629a365 --- /dev/null +++ b/add_up.rb @@ -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) diff --git a/full_name.rb b/full_name.rb new file mode 100644 index 0000000..a02a50d --- /dev/null +++ b/full_name.rb @@ -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 + + diff --git a/leap_year.rb b/leap_year.rb new file mode 100644 index 0000000..9f95abb --- /dev/null +++ b/leap_year.rb @@ -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 \ No newline at end of file diff --git a/sorted_words.rb b/sorted_words.rb new file mode 100644 index 0000000..f3c3900 --- /dev/null +++ b/sorted_words.rb @@ -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 \ No newline at end of file