# Writing power function for large numbers

We have given two numbers x and n which are base and exponent respectively. Write a function to compute x^n where 1 <= x, n <= 10000 and overflow may happen

Examples:

```Input : x = 5, n = 20
Output : 95367431640625

Input : x = 2, n = 100
Output : 1267650600228229401496703205376
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

In the above example, 2^100 has 31 digits and it is not possible to store these digits even if we use long long int which can store maximum 18 digits. The idea behind is that multiply x, n times and store result in res[] array.
Here is the algorithm for finding power of a number.

Power(n)
1. Create an array res[] of MAX size and store x in res[] array and initialize res_size as the number of digits in x.
2. Do following for all numbers from i=2 to n
…..Multiply x with res[] and update res[] and res_size to store the multiplication result.

Multiply(res[], x)
1. Initialize carry as 0.
2. Do following for i=0 to res_size-1
….a. Find prod = res[i]*x+carry.
….b. Store last digit of prod in res[i] and remaining digits in carry.
3. Store all digits of carry in res[] and increase res_size by number of digits.

## C++

 `// C++ program to compute ` `// factorial of big numbers ` `#include ` `using` `namespace` `std; ` ` `  `// Maximum number of digits in  ` `// output ` `#define MAX 100000 ` ` `  `// This function multiplies x ` `// with the number represented by res[]. ` `// res_size is size of res[] or ` `// number of digits in the number ` `// represented by res[]. This function ` `// uses simple school mathematics ` `// for multiplication. ` `// This function may value of res_size ` `// and returns the new value of res_size ` `int` `multiply(``int` `x, ``int` `res[], ``int` `res_size) { ` ` `  `// Initialize carry ` `int` `carry = 0; ` ` `  `// One by one multiply n with ` `// individual digits of res[] ` `for` `(``int` `i = 0; i < res_size; i++) { ` `    ``int` `prod = res[i] * x + carry; ` ` `  `    ``// Store last digit of ` `    ``// 'prod' in res[] ` `    ``res[i] = prod % 10; ` ` `  `    ``// Put rest in carry ` `    ``carry = prod / 10; ` `} ` ` `  `// Put carry in res and ` `// increase result size ` `while` `(carry) { ` `    ``res[res_size] = carry % 10; ` `    ``carry = carry / 10; ` `    ``res_size++; ` `} ` `return` `res_size; ` `} ` ` `  `// This function finds ` `// power of a number x ` `void` `power(``int` `x, ``int` `n)  ` `{ ` ` `  `//printing value "1" for power = 0 ` `if``(n == 0 ){  ` `    ``cout<<``"1"``; ` `    ``return``; ` `} ` ` `  ` `  `int` `res[MAX]; ` `int` `res_size = 0; ` `int` `temp = x; ` ` `  `// Initialize result ` `while` `(temp != 0) { ` `    ``res[res_size++] = temp % 10; ` `    ``temp = temp / 10; ` `} ` ` `  `// Multiply x n times ` `// (x^n = x*x*x....n times) ` `for` `(``int` `i = 2; i <= n; i++) ` `    ``res_size = multiply(x, res, res_size); ` ` `  `cout << x << ``"^"` `<< n << ``" = "``; ` `for` `(``int` `i = res_size - 1; i >= 0; i--) ` `    ``cout << res[i]; ` `} ` ` `  `// Driver program ` `int` `main() { ` `int` `exponent = 100; ` `int` `base = 20; ` `power(base, exponent); ` `return` `0; ` `} `

## Java

 `// Java program to compute ` `// factorial of big numbers ` `class` `GFG { ` `// Maximum number of digits in ` `// output ` `static` `final` `int` `MAX = ``100000``; ` ` `  `// This function multiplies x ` `// with the number represented by res[]. ` `// res_size is size of res[] or ` `// number of digits in the number ` `// represented by res[]. This function ` `// uses simple school mathematics ` `// for multiplication. ` `// This function may value of res_size ` `// and returns the new value of res_size ` `static` `int` `multiply(``int` `x, ``int` `res[], ``int` `res_size) { ` ` `  `    ``// Initialize carry ` `    ``int` `carry = ``0``; ` ` `  `    ``// One by one multiply n with ` `    ``// individual digits of res[] ` `    ``for` `(``int` `i = ``0``; i < res_size; i++) { ` `    ``int` `prod = res[i] * x + carry; ` ` `  `    ``// Store last digit of ` `    ``// 'prod' in res[] ` `    ``res[i] = prod % ``10``; ` ` `  `    ``// Put rest in carry ` `    ``carry = prod / ``10``; ` `    ``} ` ` `  `    ``// Put carry in res and ` `    ``// increase result size ` `    ``while` `(carry > ``0``) { ` `    ``res[res_size] = carry % ``10``; ` `    ``carry = carry / ``10``; ` `    ``res_size++; ` `    ``} ` `    ``return` `res_size; ` `} ` ` `  `// This function finds ` `// power of a number x ` `static` `void` `power(``int` `x, ``int` `n) { ` `     `  `    ``//printing value "1" for power = 0 ` `    ``if``(n == ``0` `){  ` `    ``System.out.print(``"1"``); ` `    ``return``; ` `} ` `    ``int` `res[] = ``new` `int``[MAX]; ` `    ``int` `res_size = ``0``; ` `    ``int` `temp = x; ` ` `  `    ``// Initialize result ` `    ``while` `(temp != ``0``) { ` `    ``res[res_size++] = temp % ``10``; ` `    ``temp = temp / ``10``; ` `    ``} ` ` `  `    ``// Multiply x n times ` `    ``// (x^n = x*x*x....n times) ` `    ``for` `(``int` `i = ``2``; i <= n; i++) ` `    ``res_size = multiply(x, res, res_size); ` ` `  `    ``System.out.print(x + ``"^"` `+ n + ``" = "``); ` `    ``for` `(``int` `i = res_size - ``1``; i >= ``0``; i--) ` `    ``System.out.print(res[i]); ` `} ` `// Driver code ` `public` `static` `void` `main(String[] args) { ` `    ``int` `exponent = ``100``; ` `    ``int` `base = ``2``; ` `    ``power(base, exponent); ` `} ` `} ` `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python program to compute ` `# factorial of big numbers ` ` `  `# Maximum number of digits in  ` `# output ` `MAX``=``100000` ` `  `# This function multiplies x ` `# with the number represented by res[]. ` `# res_size is size of res[] or ` `# number of digits in the number ` `# represented by res[]. This function ` `# uses simple school mathematics ` `# for multiplication. ` `# This function may value of res_size ` `# and returns the new value of res_size ` `def` `multiply(x, res, res_size): ` ` `  `    ``# Initialize carry ` `    ``carry ``=` `0` ` `  `    ``# One by one multiply n with ` `    ``# individual digits of res[] ` `    ``for` `i ``in` `range``(res_size): ` `        ``prod ``=` `res[i] ``*` `x ``+` `carry ` ` `  `        ``# Store last digit of ` `        ``# 'prod' in res[] ` `        ``res[i] ``=` `prod ``%` `10` ` `  `        ``# Put rest in carry ` `        ``carry ``=` `prod ``/``/` `10` ` `  `    ``# Put carry in res and ` `    ``# increase result size ` `    ``while` `(carry): ` `        ``res[res_size] ``=` `carry ``%` `10` `        ``carry ``=` `carry ``/``/` `10` `        ``res_size``+``=``1` ` `  `    ``return` `res_size ` ` `  ` `  `# This function finds ` `# power of a number x ` `def` `power(x,n): ` `     `  `    ``# printing value "1" for power = 0 ` `     ``if` `(n ``=``=` `0``) :  ` `    ``print``(``"1"``) ` `    ``return` `     `  `    ``res``=``[``0` `for` `i ``in` `range``(``MAX``)] ` `    ``res_size ``=` `0` `    ``temp ``=` `x ` ` `  `    ``# Initialize result ` `    ``while` `(temp !``=` `0``): ` `        ``res[res_size] ``=` `temp ``%` `10``; ` `        ``res_size``+``=``1` `        ``temp ``=` `temp ``/``/` `10` ` `  ` `  `    ``# Multiply x n times ` `    ``# (x^n = x*x*x....n times) ` `    ``for` `i ``in` `range``(``2``, n ``+` `1``): ` `        ``res_size ``=` `multiply(x, res, res_size) ` ` `  `    ``print``(x , ``"^"` `, n , ``" = "``,end``=``"") ` `    ``for` `i ``in` `range``(res_size ``-` `1``, ``-``1``, ``-``1``): ` `        ``print``(res[i], end``=``"") ` ` `  ` `  `# Driver program ` ` `  `exponent ``=` `100` `base ``=` `2` `power(base, exponent) ` ` `  `# This code is contributed ` `# by Anant Agarwal. `

## C#

 `// C# program to compute ` `// factorial of big numbers ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Maximum number of digits in ` `    ``// output ` `    ``static` `int` `MAX = 100000; ` `     `  `    ``// This function multiplies x ` `    ``// with the number represented by res[]. ` `    ``// res_size is size of res[] or ` `    ``// number of digits in the number ` `    ``// represented by res[]. This function ` `    ``// uses simple school mathematics ` `    ``// for multiplication. ` `    ``// This function may value of res_size ` `    ``// and returns the new value of res_size ` `    ``static` `int` `multiply(``int` `x, ``int` `[]res,  ` `                            ``int` `res_size) ` `    ``{ ` `     `  `        ``// Initialize carry ` `        ``int` `carry = 0; ` `     `  `        ``// One by one multiply n with ` `        ``// individual digits of res[] ` `        ``for` `(``int` `i = 0; i < res_size; i++) ` `        ``{ ` `            ``int` `prod = res[i] * x + carry; ` `         `  `            ``// Store last digit of ` `            ``// 'prod' in res[] ` `            ``res[i] = prod % 10; ` `         `  `            ``// Put rest in carry ` `            ``carry = prod / 10; ` `        ``} ` `     `  `        ``// Put carry in res and ` `        ``// increase result size ` `        ``while` `(carry > 0) ` `        ``{ ` `            ``res[res_size] = carry % 10; ` `            ``carry = carry / 10; ` `            ``res_size++; ` `        ``} ` `         `  `        ``return` `res_size; ` `    ``} ` `     `  `    ``// This function finds ` `    ``// power of a number x ` `    ``static` `void` `power(``int` `x, ``int` `n) ` `    ``{ ` `        ``//printing value "1" for power = 0 ` `    ``if``(n == 0 ){  ` `    ``Console.Write(``"1"``); ` `    ``return``; ` `    ``} ` `        ``int` `[]res = ``new` `int``[MAX]; ` `        ``int` `res_size = 0; ` `        ``int` `temp = x; ` `     `  `        ``// Initialize result ` `        ``while` `(temp != 0) { ` `            ``res[res_size++] = temp % 10; ` `            ``temp = temp / 10; ` `        ``} ` `     `  `        ``// Multiply x n times ` `        ``// (x^n = x*x*x....n times) ` `        ``for` `(``int` `i = 2; i <= n; i++) ` `            ``res_size = multiply(x, res, res_size); ` `     `  `        ``Console.Write(x + ``"^"` `+ n + ``" = "``); ` `         `  `        ``for` `(``int` `i = res_size - 1; i >= 0; i--) ` `            ``Console.Write(res[i]); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `exponent = 100; ` `        ``int` `b_ase = 2; ` `        ``power(b_ase, exponent); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 `= 0; ``\$i``--, ``\$O``++) ` `if``(``\$res``[``\$i``]) ` `break``; ` `for` `(``\$i` `= ``count``(``\$res``) - ``\$O` `- 1;  ` `            ``\$i` `>= 0; ``\$i``--) ` `    ``echo` `\$res``[``\$i``]; ` `} ` ` `  `// Driver Code ` `\$exponent` `= 100; ` `\$base` `= 2; ` `power(``\$base``, ``\$exponent``); ` ` `  `// This code is contributed  ` `// by mits ` `?> `

Output:

```2^100 = 1267650600228229401496703205376
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : Mithun Kumar, AmanBhagat

Article Tags :
Practice Tags :

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.