Last active
August 29, 2015 14:01
-
-
Save StephenFiser/faec479d6a3430e25dd6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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