Skip to content

Instantly share code, notes, and snippets.

@ph3nx
Last active January 3, 2016 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ph3nx/8490866 to your computer and use it in GitHub Desktop.
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.
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