Java Reference
In-Depth Information
Table 22-3
Methods in the BigInteger Class (continued)
RETURNS
METHOD/ACTION
String
toString()
Returns the decimal String representation of this
BigInteger
String
toString(int radix)
Returns the String representation of this
BigIntegerin the given radix
Static
BigInteger valueOf(long val)
Returns a BigInteger whose value is equal to
that of the specified long
BigInteger
xor(BigInteger val)
Returns a BigInteger whose value is (this ^ val)
A sample program
Thefollowingprogramdemonstratesarithmeticonbigintegernumbersby
calculating the factorial. The following is a listing of the source code.
// Filename: BigIFact
// Reference: Chapter 22
// Description:
//
Demonstration of high-precision integer arithmetic
//
with the BigInteger class. Program calculates the
//
factorial of a big integer number
// Requires:
//
Keyin class in current directory
import java.math.*;
class BigIFact
{
public static void main(String[] args)
{
int v; // Input
BigInteger p = BigInteger.valueOf(1); // Factor
System.out.println("Big integer factorial routine\n");
v = Keyin.inInt("Enter value: ");
// Calculate factorial by iteration
for(inti=1;i<=v;i++)
p = p.multiply(BigInteger.valueOf(i));
// Display result
System.out.println(p);
}
}
Search WWH ::




Custom Search