- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Program to illustrate the working of our solution,
Example
#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
- Related Articles
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- If ( 2 x+y=23 ) and ( 4 x-y=19 ), find the values of ( 5 y-2 x ) and ( frac{y}{x}-2 ).
- Find x and y."
- Find the sum of the following arithmetic progressions:( frac{x-y}{x+y}, frac{3 x-2 y}{x+y}, frac{5 x-3 y}{x+y}, ldots ) to ( n ) terms
- Find $25 x^{2}+16 y^{2}$, if $5 x+4 y=8$ and $x y=1$.
- Verify : (i) ( x^{3}+y^{3}=(x+y)left(x^{2}-x y+y^{2}right) )(ii) ( x^{3}-y^{3}=(x-y)left(x^{2}+x y+y^{2}right) )
- Factorize:$4(x - y)^2 - 12(x -y) (x + y) + 9(x + y)^2$
- In ( Delta X Y Z, X Y=X Z ). A straight line cuts ( X Z ) at ( P, Y Z ) at ( Q ) and ( X Y ) produced at ( R ). If ( Y Q=Y R ) and ( Q P=Q Z ), find the angles of ( Delta X Y Z ).
- LCM of two prime number $x$ and $y( x>y)$ is $161$. Find value of $3y-x$.
- Find $x, y$ and $z$."
- Factorize:( x^{2}+y-x y-x )
- If $frac{x+1}{y} = frac{1}{2}, frac{x}{y-2} = frac{1}{2}$, find x and y.
- If $x=34$ and $y=x$, what is the value of $x+y$?
- Subtract $24 x y-10 y-18 x$ from $30 x y +12 y+14 x$

Advertisements