java.math.BigInteger pow(int exponent)
Description
On this document we will be showing a java example on how to use the pow(int exponent) method of BigInteger Class. Basically this method returns a BigInteger whose value is (thisexponent). Note that exponent is an integer rather than a BigInteger.
Notes:
- this method returns an ArithmeticException if the exponent specified is negative for the reason that this scenario will yield to non-integer result.
Method Syntax
public BigInteger pow(int exponent)
Method Argument
Data Type | Parameter | Description |
---|---|---|
int | exponent | exponent to which this BigInteger is to be raised. |
Method Returns
The pow() method returns thisexponent
Compatibility
Requires Java 1.1 and up
Java BigInteger pow() Example
Below is a java code demonstrates the use of pow(int exponent) method of BigInteger class. The example presented might be simple however it shows the behavior of the pow(int exponent) method.
package com.javatutorialhq.java.examples; import java.math.BigInteger; import java.util.Scanner; /* * A java example source code to demonstrate * the use of pow() method of BigInteger class */ public class BigIntegerPowExample { public static void main(String[] args) { // ask for the base number from user System.out.print("Enter the base number:"); Scanner s = new Scanner(System.in); String input = s.nextLine(); // ask for exponent System.out.print("Enter the exponent:"); String exponent = s.nextLine(); s.close(); // convert the string input to BigInteger BigInteger value = new BigInteger(input); Integer exp = Integer.parseInt(exponent); // get result BigInteger result = value.pow(exp); System.out.println("The result is "+result); } }
This example is a lot simpler than it looks. We simply ask the user for two inputs and convert these into BigInteger. The first input will be the base number while the second input is the exponent on which our base number will be raised.