Given two integer numbers X and Y. find the larger of X^Y and Y^X, or determine if they are equal.

**Examples:**

Input : 2 3 Output : 3^2 We know 3^2 = 9 and 2^3 = 8. Input : 2 4 Output : Equal

A **simple solution **is calculate x^y by looping for y times, but if the values of x and y is too large it will cause overflow.

To solve the overflow problem, we can simplify the equation by taking log.

log(x^y) = y* log(x)

Now, this equation will not cause overflow and we can compare two values directly.

## C++

`// C++ program to print greater of x^y and ` `// y^x ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `printGreater(` `double` `x, ` `double` `y) ` `{ ` ` ` `long` `double` `X = y * ` `log` `(x); ` ` ` `long` `double` `Y = x * ` `log` `(y); ` ` ` `if` `(` `abs` `(X - Y) < 1e-9) { ` ` ` `cout << ` `"Equal"` `; ` ` ` `} ` ` ` `else` `if` `(X > Y) { ` ` ` `cout << x << ` `"^"` `<< y; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << y << ` `"^"` `<< x; ` ` ` `} ` `} ` ` ` `int` `main() ` `{ ` ` ` `double` `x = 5, y = 8; ` ` ` `printGreater(x, y); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print ` `// greater of x^y and y^x ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` `static` `void` `printGreater(` `int` `x, ` ` ` `int` `y) ` `{ ` ` ` `double` `X = y * Math.log(x); ` ` ` `double` `Y = x * Math.log(y); ` ` ` `if` `(Math.abs(X - Y) < 1e-` `9` `) ` ` ` `{ ` ` ` `System.out.println(` `"Equal"` `); ` ` ` `} ` ` ` `else` `if` `(X > Y) ` ` ` `{ ` ` ` `System.out.println(x + ` `"^"` `+ y); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `System.out.println(y + ` `"^"` `+ x); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `x = ` `5` `, y = ` `8` `; ` ` ` `printGreater(x, y); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to print greater ` `# of x^y and y^x ` `import` `math ` ` ` `def` `printGreater(x, y): ` ` ` ` ` `X ` `=` `y ` `*` `math.log(x); ` ` ` `Y ` `=` `x ` `*` `math.log(y); ` ` ` `if` `(` `abs` `(X ` `-` `Y) < ` `1e` `-` `9` `): ` ` ` `print` `(` `"Equal"` `); ` ` ` `elif` `(X > Y): ` ` ` `print` `(x, ` `"^"` `, y); ` ` ` `else` `: ` ` ` `print` `(y, ` `"^"` `, x); ` ` ` `# Driver Code ` `x ` `=` `5` `; ` `y ` `=` `8` `; ` `printGreater(x, y); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print ` `// greater of x^y and y^x ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `void` `printGreater(` `int` `x, ` ` ` `int` `y) ` `{ ` ` ` `double` `X = y * Math.Log(x); ` ` ` `double` `Y = x * Math.Log(y); ` ` ` `if` `(Math.Abs(X - Y) < 1e-9) ` ` ` `{ ` ` ` `Console.WriteLine(` `"Equal"` `); ` ` ` `} ` ` ` `else` `if` `(X > Y) ` ` ` `{ ` ` ` `Console.WriteLine(x + ` ` ` `"^"` `+ y); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `Console.WriteLine(y + ` ` ` `"^"` `+ x); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `x = 5, y = 8; ` ` ` `printGreater(x, y); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to print greater ` `// of x^y and y^x ` `function` `printGreater(` `$x` `, ` `$y` `) ` `{ ` ` ` `$X` `= ` `$y` `* log(` `$x` `); ` ` ` `$Y` `= ` `$x` `* log(` `$y` `); ` ` ` `if` `(` `abs` `(` `$X` `- ` `$Y` `) < 1e-9) ` ` ` `{ ` ` ` `echo` `"Equal"` `; ` ` ` `} ` ` ` `else` `if` `(` `$X` `> ` `$Y` `) ` ` ` `{ ` ` ` `echo` `$x` `. ` `"^"` `. ` `$y` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `echo` `$y` `. ` `"^"` `. ` `$x` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `$x` `= 5; ` `$y` `= 8; ` `printGreater(` `$x` `, ` `$y` `); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

5^8

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the larger exponential among two exponentials
- Check if a larger number divisible by 36
- Smallest triangular number larger than p
- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Sum of N-terms of geometric progression for larger values of N | Set 2 (Using recursion)
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Find smallest positive number Y such that Bitwise AND of X and Y is Zero
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Program to find if two numbers and their AM and HM are present in an array using STL
- Find a number M < N such that difference between their XOR and AND is maximum
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Find Union and Intersection of two unsorted arrays
- Find the largest multiple of 2, 3 and 5

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.