# Find larger of x^y and y^x in C++

In this problem, we are given two numbers x and y. Our task is to find larger of x^y and y^x.

Problem Description: The problem is simple, we need to find weather x to the power y is greater than y to the power x.

Let’s take an example to understand the problem,

Input: x = 4, y = 5

Output: 1024

Explanation:

x^y = 4^5 = 1024
y^x = 5^4 = 625

## Solution Approach

The solution to the problem is simple. We need to find the value of x^y and y^x and return the maximum of both.

There can be a more mathematically easy way to solve the problem, which is by taking log. So,

x^y = y*log(x)

These values are easy to calculate.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;

int main() {

double x = 3, y = 7;
double ylogx = y * log(x);
double xlogy = x * log(y);

if(ylogx > xlogy)
cout<<x<<"^"<<y;
else if (ylogx < xlogy)
cout<<y<<"^"<<x;
else
cout<<"None";
cout<<" has greater value";
return 0;
}

## Output

3^7 has greater value

Updated on: 25-Jan-2021

111 Views