- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Larger of a^b or b^a in C++

In this tutorial, we are going to write a program that finds out the larger among the a^{b} and b^{a}

It's a straightforward problem. Let's see the steps to solve it.

- Initialise the values of a and b.
- Take the log of both the values.
- Compute the values of $b\:\log\:a$ and $a\:\log\:b$
- Compare the both values.
- If $a\:\log\:b$ is greater than $b\:\log\:a$, then print b
^{a}is greater. - If $b\:\log\:a$ is greater than $a\:\log\:b$, then print a
^{b}is greater. - Else print both are equal.

## Example

Let's see the code.

#include <bits/stdc++.h> using namespace std; int main() { int a = 4, b = 7; 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; } return 0; }

## Output

If you run the above code, then you will get the following result.

a ^ b is greater

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.

- Related Questions & Answers
- Find FIRST & FOLLOW for the following Grammar. S → A a A | B b B A → b B B → ε
- Find multiple of x closest to or a ^ b (a raised to power b) in C++
- Python a += b is not always a = a + b
- Show that the following grammar is LR (1) S → A a |b A c |B c | b B a A → d B → d
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++
- Construct a Finite Automata for the regular expression ((a+b)(a+b))*.
- Count total divisors of A or B in a given range in C++
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Number of Digits in a^b in C++
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N) in C++
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Find all pairs (a, b) in an array such that a % b = k in C++
- A += B Assignment Riddle in Python
- Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++

Advertisements