Skip to content

Instantly share code, notes, and snippets.

@StephenFiser
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save StephenFiser/faec479d6a3430e25dd6 to your computer and use it in GitHub Desktop.

Select an option

Save StephenFiser/faec479d6a3430e25dd6 to your computer and use it in GitHub Desktop.
p "Enter a number: "
num1 = gets.chomp.to_i
p "Enter an exponent: "
num2 = gets.chomp.to_i
def exponent(number, exp)
answer = number
if exp == 0
answer = 1
else
(exp - 1).times do |i|
# for 2 ^ 2, the 2 is multiplied by itself 1 time, or 2 - 1
# in the general case, n ^ m is n multiplied by itself m - 1 times
answer *= number
p answer
end
end
answer
end
answer = exponent(num1, num2)
p "#{num1} to the power #{num2} equals #{answer}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment