[CSC 204] Lab 7
Andrew J. Pounds
pounds_aj at mercer.edu
Thu Oct 13 18:28:44 EDT 2016
Several of you still seem to be struggling with Lab 7 and the BigInteger
class. Most of the problems I have seen have been the result of people
not comparing things properly. Here is my solution...
public class BiFactorial {
public static void main(String[] args) {
BigInteger value; // The value being computed
Scanner in = new Scanner (System.in); // To allow for input
// Get the first value
System.out.print("A value, please (use 0 to stop): ");
value = BigInteger.valueOf(in.nextInt());
while (!value.equals(BigInteger.valueOf(0))) {
BigInteger result = fact(value);
System.out.println(value + "! = " + result);
System.out.print("A value, please (use 0 to stop): ");
value = in.nextBigInteger();
}
}
private static BigInteger fact(BigInteger n) {
BigInteger product = BigInteger.ONE;
for (int i = 2; (n.compareTo(BigInteger.valueOf(i)) >= 0); i++) {
product = product.multiply(BigInteger.valueOf(i));
}
return product;
}
}
--
Andrew J. Pounds, Ph.D. (pounds_aj at mercer.edu)
Professor of Chemistry and Computer Science
Mercer University, Macon, GA 31207 (478) 301-5627
http://faculty.mercer.edu/pounds_aj
More information about the csc204
mailing list