Last active
January 3, 2016 16:39
-
-
Save ph3nx/8490866 to your computer and use it in GitHub Desktop.
Ruby program that asks the user for a number num and computes either the sum or the product of all numbers starting at 1 until this number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sum_or_product | |
print 'Enter any number bigger 1: ' | |
num = gets.to_i | |
print 'sum [s] or product [p]? ' | |
opt = gets.chomp | |
if opt == 's' | |
(1..num).inject :+ | |
elsif opt == 'p' | |
(1..num).inject :* | |
else | |
p "There is no option like '#{opt}', start again.." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment