# Larger of a^b or b^a (a raised to power b or b raised to power a)

Given two numbers , find which is greater .

If , print a^b is greater

If , print b^a is greater

If , print Both are equal

**Examples:**

Input : 3 5 Output : a^b is greater 3^5 = 243, 5^3 = 125. Since, 243>125, therefore a^b > b^a. Input : 2 4 Output : Both are equal 2^4 = 16, 4^2 = 16. Since, 16=16, therefore a^b = b^a.

**Brute Force** solution would be to just compute and compare them. But since can be large enough that can not be stored even in long long int, so this solution is not feasible. Also computing to the power n would require at least time using the fast exponentiation technique.

**Efficient approach** would be to use logarithm. We have to compare . If we take log, the problem reduces to comparing .

Hence,

If , print a^b is greater

If , print b^a is greater

If , print Both are equal

Below is the implementation of the efficient approach discussed above.

## C++

`// C++ code for finding greater ` `// between the a^b and b^a ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the greater value ` `void` `findGreater(` `int` `a, ` `int` `b) ` `{ ` ` ` `long` `double` `x = (` `long` `double` `)a * (` `long` `double` `)(` `log` `((` `long` `double` `)(b))); ` ` ` `long` `double` `y = (` `long` `double` `)b * (` `long` `double` `)(` `log` `((` `long` `double` `)(a))); ` ` ` `if` `(y > x) { ` ` ` `cout << ` `"a^b is greater"` `<< endl; ` ` ` `} ` ` ` `else` `if` `(y < x) { ` ` ` `cout << ` `"b^a is greater"` `<< endl; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"Both are equal"` `<< endl; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 3, b = 5, c = 2, d = 4; ` ` ` `findGreater(a, b); ` ` ` `findGreater(c, d); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code for finding greater ` `// between the a^b and b^a ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// Function to find the greater value ` ` ` `static` `void` `findGreater(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `double` `x = (` `double` `)a * (` `double` `)(Math.log((` `double` `)(b))); ` ` ` `double` `y = (` `double` `)b * (` `double` `)(Math.log((` `double` `)(a))); ` ` ` `if` `(y > x) { ` ` ` `System.out.println(` `"a^b is greater"` `) ; ` ` ` `} ` ` ` `else` `if` `(y < x) { ` ` ` `System.out.println(` `"b^a is greater"` `) ; ` ` ` `} ` ` ` `else` `{ ` ` ` `System.out.println(` `"Both are equal"` `) ; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` `int` `a = ` `3` `, b = ` `5` `, c = ` `2` `, d = ` `4` `; ` ` ` `findGreater(a, b); ` ` ` `findGreater(c, d); ` ` ` `} ` ` ` `// This code is contributed by Ryuga ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 code for finding greater ` `# between the a^b and b^a ` `import` `math ` ` ` `# Function to find the greater value ` `def` `findGreater(a, b): ` ` ` ` ` `x ` `=` `a ` `*` `(math.log(b)); ` ` ` `y ` `=` `b ` `*` `(math.log(a)); ` ` ` `if` `(y > x): ` ` ` `print` `(` `"a^b is greater"` `); ` ` ` `elif` `(y < x): ` ` ` `print` `(` `"b^a is greater"` `); ` ` ` `else` `: ` ` ` `print` `(` `"Both are equal"` `); ` ` ` `# Driver code ` `a ` `=` `3` `; ` `b ` `=` `5` `; ` `c ` `=` `2` `; ` `d ` `=` `4` `; ` `findGreater(a, b); ` `findGreater(c, d); ` ` ` `# This code is contributed ` `# by Shivi_Aggarwal ` |

*chevron_right*

*filter_none*

## C#

`// C# code for finding greater ` `// between the a^b and b^a ` ` ` `using` `System; ` `public` `class` `GFG{ ` ` ` ` ` `// Function to find the greater value ` ` ` `static` `void` `findGreater(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `double` `x = (` `double` `)a * (` `double` `)(Math.Log((` `double` `)(b))); ` ` ` `double` `y = (` `double` `)b * (` `double` `)(Math.Log((` `double` `)(a))); ` ` ` `if` `(y > x) { ` ` ` `Console.Write(` `"a^b is greater\n"` `) ; ` ` ` `} ` ` ` `else` `if` `(y < x) { ` ` ` `Console.Write(` `"b^a is greater"` `+` `"\n"` `) ; ` ` ` `} ` ` ` `else` `{ ` ` ` `Console.Write(` `"Both are equal"` `) ; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `a = 3, b = 5, c = 2, d = 4; ` ` ` `findGreater(a, b); ` ` ` `findGreater(c, d); ` ` ` `} ` ` ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code for finding greater ` `// between the a^b and b^a ` ` ` `// Function to find the greater value ` `function` `findGreater(` `$a` `, ` `$b` `) ` `{ ` ` ` `$x` `= (double)` `$a` `* (double)(log((double)(` `$b` `))); ` ` ` `$y` `= (double)` `$b` `* (double)(log((double)(` `$a` `))); ` ` ` `if` `(` `$y` `> ` `$x` `) ` ` ` `{ ` ` ` `echo` `"a^b is greater"` `, ` `"\n"` `; ` ` ` `} ` ` ` `else` `if` `(` `$y` `< ` `$x` `) ` ` ` `{ ` ` ` `echo` `"b^a is greater"` `, ` `"\n"` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `echo` `"Both are equal"` `, ` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `$a` `= 3; ` `$b` `= 5; ` `$c` `= 2; ` `$d` `= 4; ` `findGreater(` `$a` `, ` `$b` `); ` `findGreater(` `$c` `, ` `$d` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

a^b is greater Both are equal

**Time Complexity:**

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Find the sum of power of bit count raised to the power B
- K-th digit in 'a' raised to power 'b'
- Find value of y mod (2 raised to power x)
- Print last k digits of a^b (a raised to power b)
- Number of digits in 2 raised to power n
- Find unit digit of x raised to power y
- Find multiple of x closest to or a ^ b (a raised to power b)
- Check if a number can be expressed as x^y (x raised to power y)
- Find last five digits of a given five digit number raised to power five
- Minimum removals in a number to be divisible by 10 power raised to K
- GCD of a number raised to some power and another number
- Compute power of power k times % m
- Check if given number is a power of d where d is a power of 2
- Find power of power under mod of a prime
- Sum of each element raised to (prime-1) % prime

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.