Last active
December 24, 2018 07:13
-
-
Save kylelong/1b64d15a1d6c1430cabd9bd3b1477c3f to your computer and use it in GitHub Desktop.
Return string form of power function
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
/** | |
* Created by kylel95 on 12/24/18. | |
*/ | |
public class greaterPower { | |
public static void main(String [] args){ | |
int a = 3; | |
int b = 5; | |
int c = 2; | |
int d = 4; | |
System.out.println(greaterPow(a, b)); | |
System.out.println(greaterPow(c, d)); | |
} | |
/** | |
* Raise a to the b power | |
* @param a base | |
* @param b exponenet | |
* @return a ^ b | |
*/ | |
public static int pow(int a, int b){ | |
int n = 1; | |
for(int i = 0; i < b; i++){ | |
n *= a; | |
} | |
return n; | |
} | |
/** | |
* Prints out result string from pow(a,b) | |
* @param a first int | |
* @param b second int | |
* @return string detailing results from pow(a,b) | |
*/ | |
public static String greaterPow(int a, int b){ | |
int first = pow(a, b); | |
int second = pow(b, a); | |
if(first > second){ | |
return "a^b is greater."; | |
} | |
else if(second > first){ | |
return "b^a is greater."; | |
} | |
return "Both are equal."; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment