Given three integers a, b and n, the task is to find out greater value between a^{n} and b^{n}.

**Examples:**

Input:a = 3, b = 4, n = 5

Output:b^n is greater than a^n

Value of a^{n}is 243 and the value of b^{n}is 1024.

So, b^{n}is greater than a^{n}.

Input:a = -3, b = 2, n = 4

Output:a^n is greater than b^n

Value of a^{n}is 243 and the value of b^{n}is 16.

So, a^{n}is greater than b^{n}.

**Basic Approach:** For every value of a, b and n, calculate the values of a^{n} and b^{n}. Then compare the result obtained and display the according to output.

The problem with this approach arises when there are large values of a, b and n. For large values of a, n, calculating a^{n} can exceed the limit of integer which will cause integer overflow.

**Better approach** is to check the value of n.

- If n is even then calculate the absolute value of a and b.
- If n is odd then take the given value as it is.
- Now check if a is equal to b. If yes, print 0.
- If a is greater than b, print 1.
- Otherwise, print 2.

## C++

`// C++ code for finding greater ` `// between the a^n and b^n ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the greater value ` `void` `findGreater(` `int` `a, ` `int` `b, ` `int` `n) ` `{ ` ` ` `// If n is even ` ` ` `if` `(!(n & 1)) { ` ` ` ` ` `a = ` `abs` `(a); ` ` ` `b = ` `abs` `(b); ` ` ` `} ` ` ` `if` `(a == b) ` ` ` `cout << ` `"a^n is equal to b^n"` `; ` ` ` ` ` `else` `if` `(a > b) ` ` ` `cout << ` `"a^n is greater than b^n"` `; ` ` ` ` ` `else` ` ` `cout << ` `"b^n is greater than a^n"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 12, b = 24, n = 5; ` ` ` `findGreater(a, b, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA code for finding greater ` `// between the a^n and b^n ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find ` ` ` `// the greater value ` ` ` `static` `void` `findGreater(` `int` `a, ` ` ` `int` `b, ` `int` `n) ` `{ ` ` ` `// If n is even ` ` ` `if` `(!((n & ` `1` `) > ` `0` `)) ` ` ` `{ ` ` ` ` ` `a = Math.abs(a); ` ` ` `b = Math.abs(b); ` ` ` `} ` ` ` `if` `(a == b) ` ` ` `System.out.println(` `"a^n is "` `+ ` ` ` `"equal to b^n"` `); ` ` ` ` ` `else` `if` `(a > b) ` ` ` `System.out.println(` `"a^n is greater "` `+ ` ` ` `"than b^n"` `); ` ` ` ` ` `else` ` ` `System.out.println(` `"b^n is greater "` `+ ` ` ` `"than a^n"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` `int` `a = ` `12` `, b = ` `24` `, n = ` `5` `; ` `findGreater(a, b, n); ` `} ` `} ` ` ` `// This code is contributed ` `// by shiv_bhakt. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code for finding greater ` `# between the a^n and b^n ` `import` `math ` ` ` `# Function to find the greater value ` `def` `findGreater(a, b, n): ` ` ` ` ` `# If n is even ` ` ` `if` `((n & ` `1` `) > ` `0` `): ` ` ` ` ` `a ` `=` `abs` `(a); ` ` ` `b ` `=` `abs` `(b); ` ` ` `if` `(a ` `=` `=` `b): ` ` ` `print` `(` `"a^n is equal to b^n"` `); ` ` ` ` ` `elif` `(a > b): ` ` ` `print` `(` `"a^n is greater than b^n"` `); ` ` ` ` ` `else` `: ` ` ` `print` `(` `"b^n is greater than a^n"` `); ` ` ` `# Driver code ` `a ` `=` `12` `; ` `b ` `=` `24` `; ` `n ` `=` `5` `; ` `findGreater(a, b, n); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# code for finding greater ` `// between the a^n and b^n ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find ` ` ` `// the greater value ` ` ` `static` `void` `findGreater(` `int` `a, ` ` ` `int` `b, ` ` ` `int` `n) ` ` ` `{ ` ` ` `// If n is even ` ` ` `if` `(!((n & 1) > 0)) ` ` ` `{ ` ` ` ` ` `a = Math.Abs(a); ` ` ` `b = Math.Abs(b); ` ` ` `} ` ` ` ` ` `if` `(a == b) ` ` ` `Console.WriteLine(` `"a^n is "` `+ ` ` ` `"equal to b^n"` `); ` ` ` ` ` `else` `if` `(a > b) ` ` ` `Console.WriteLine(` `"a^n is greater "` `+ ` ` ` `"than b^n"` `); ` ` ` ` ` `else` ` ` `Console.WriteLine(` `"b^n is greater "` `+ ` ` ` `"than a^n"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `a = 12, b = 24, n = 5; ` ` ` `findGreater(a, b, n); ` `} ` `} ` ` ` `// This code is contributed ` `// by shiv_bhakt. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code for finding greater ` `// between the a^n and b^n ` ` ` `// Function to find ` `// the greater value ` `function` `findGreater(` `$a` `, ` `$b` `, ` `$n` `) ` `{ ` ` ` `// If n is even ` ` ` `if` `(!(` `$n` `& 1)) ` ` ` `{ ` ` ` ` ` `$a` `= ` `abs` `(` `$a` `); ` ` ` `$b` `= ` `abs` `(` `$b` `); ` ` ` `} ` ` ` `if` `(` `$a` `== ` `$b` `) ` ` ` `echo` `"a^n is equal to b^n"` `; ` ` ` ` ` `else` `if` `(` `$a` `> ` `$b` `) ` ` ` `echo` `"a^n is greater than b^n"` `; ` ` ` ` ` `else` ` ` `echo` `"b^n is greater than a^n"` `; ` `} ` ` ` `// Driver code ` `$a` `= 12; ` `$b` `= 24; ` `$n` `= 5; ` `findGreater(` `$a` `, ` `$b` `, ` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

b^n is greater than a^n

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:

- Smallest subarray of size greater than K with sum greater than a given value
- Count numbers with difference between number and its digit sum greater than specific value
- Find minimum value to assign all array elements so that array product becomes greater
- Count number of substrings with numeric value greater than X
- Minimum value to be assigned to the elements so that sum becomes greater than initial sum
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Least root of given quadratic equation for value greater than equal to K
- Find the maximum possible value of the minimum value of modified array
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Longest subarray in which absolute difference between any two element is not greater than X
- Split Array into min number of subsets with difference between each pair greater than 1
- Program to Find the value of cos(nΘ)
- Program to find the value of tan(nΘ)
- Program to find the value of sin(nΘ)
- Program to find the Depreciation of Value
- Program to find value of 1^k + 2^k + 3^k + ... + n^k
- Program to find absolute value of a given number
- Count ways to divide C in two parts and add to A and B to make A strictly greater than B
- Highest and Smallest power of K less than and greater than equal to N respectively

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.