- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Advertisements