From 7011e7def4c30cadf8ac5d03cb969fb566fcc110 Mon Sep 17 00:00:00 2001 From: rojina <125151480+rojina77@users.noreply.github.com> Date: Wed, 18 Oct 2023 19:30:45 -0700 Subject: [PATCH] Add files via upload --- add_up.rb | 15 +++++++++++++++ full_name.rb | 19 +++++++++++++++++++ leap_year.rb | 16 ++++++++++++++++ sorted_out.rb | 22 ++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 add_up.rb create mode 100644 full_name.rb create mode 100644 leap_year.rb create mode 100644 sorted_out.rb diff --git a/add_up.rb b/add_up.rb new file mode 100644 index 0000000..16d1589 --- /dev/null +++ b/add_up.rb @@ -0,0 +1,15 @@ +#Write a program with a function add_up(i) . +def add_up(i) + sum = 0 + for num in 1..i + sum += num + end + return sum +end +result1 = add_up(6) +result2 = add_up(10) +result3 = add_up(15) + +puts "the sum of 1 to 6 = #{result1}" +puts "the sum of 1 to 10 = #{result2}" +puts "the sum of 1 to 15 = #{result3}" \ No newline at end of file diff --git a/full_name.rb b/full_name.rb new file mode 100644 index 0000000..2071f3c --- /dev/null +++ b/full_name.rb @@ -0,0 +1,19 @@ + +contact_list = [] +def ask (question, kind = "string") + print question + "" + answer = gets.chomp + answer = answer.to_i if kind == "number" + return answer +end + +answer = ask ("what is your first name?") +contact_list << answer +answer = ask ("waht is your middle name?") +contact_list << answer +answer = ask ("what is your last name") +contact_list << answer +puts "Hello, #{contact_list.join('')}!" + + contact_list = [] + diff --git a/leap_year.rb b/leap_year.rb new file mode 100644 index 0000000..cc62589 --- /dev/null +++ b/leap_year.rb @@ -0,0 +1,16 @@ +def is_leap_year(year) + (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) + end + + print "Enter starting year: " + start_year = gets.chomp.to_i + + print "Enter ending year: " + end_year = gets.chomp.to_i + + puts "Leap years between #{start_year} and #{end_year}:" + + (start_year..end_year).each do |year| + puts year if is_leap_year(year) + end + \ No newline at end of file diff --git a/sorted_out.rb b/sorted_out.rb new file mode 100644 index 0000000..ec241ea --- /dev/null +++ b/sorted_out.rb @@ -0,0 +1,22 @@ +#1 prompt user +#2 gather input (string) +#3 loop until blank word +#4 sort array +#5 print array + +array = [] +input = "a" + +until input.empty? + puts "Add a word:" + input = gets.chomp + array << input unless input.empty? +end + +sorted_array = array.sort + +puts "Sorted words:" +sorted_array.each do |word| + puts word +end +