Created
October 27, 2012 09:36
-
-
Save kennyt/3963767 to your computer and use it in GitHub Desktop.
02_calculator
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 add (num1, num2) | |
num1 + num2 | |
end | |
def subtract (num1, num2) | |
num1 - num2 | |
end | |
def sum (array) | |
answer = 0 | |
array.each {|number| answer = answer + number} | |
return answer | |
end | |
def multiply (num1, num2, option1 = 1, option2 = 1, option3 = 1, option4 = 1) | |
num1 * num2 * option1 * option2 * option3 * option4 | |
end | |
def power (num, power) | |
return 1 if power == 0 | |
answer = num | |
(power - 1).times {answer = answer * num} | |
return answer | |
end | |
def factorial (num) | |
return 1 if num == 0 | |
answer = num | |
until num == 1 do | |
answer = answer * (num - 1) | |
num -= 1 | |
end | |
return answer | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment