Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Preview File Edit View Go Tools Window Help Preview File Edit View Go Tools Wind

ID: 3858077 • Letter: P

Question

Preview File Edit View Go Tools Window Help Preview File Edit View Go Tools Window Help *+ 52%H, Mon Jul 17 11:20:29 PM aE ODragon Ball Introduction_to..java_programming-by,Daniel-Liang-10th-edition.pdf (page 408 of ,345) 11 a biginteger C https Sort by: Search Rank Page Order Search Rank Page Order Found on 24 pages Done Apps YouTube home Chegg 14 matches Page 385 java.math.BigInteger x 10.9 The BigInteger and BigDecimal Classes 385 The output is 1 8446744073709551614. There is no limit to the precision of a BigDecimal object. The divide method may throw an ArithneticException if the result cannot be terminated. However, you can use the overloaded divide(BigDecinal d, int scale, int roundingNode) method to spee- ify a scale and rounding mode to avoid this exception, where scal e is the maximum number of digits after the decimal point. For example, the following code creales two BigDecimal objects and performs division with scale 20 and rounding mode BigDecinal.ROUND UP 12 maTCcnes BigInteger a new Biginteger( 922337203685 4775807"); BigInteger b = new Biginteger eger (-2%..You c 2": You C Page 695 All the wrapper classes for primi-tive type values, java.math.Biginteger, Java... BigDecimal a = new BigDecimal(1.0); BigDecial b- new BigDecinal(3); BigDecimal c-a.divide(b, 20, BigDecimal.ROUND UP) Systen.out.print1n(c); 1 match The output is 0.333333 33333333333334. Nole that the factorial of an integer can be very large. Listing 10.9 gives a method that can b matcnes the factorial of any integer LISTING 10.9 LargeFactorial.java 1 port java.math. 3 public class LargeFactorial return the factorial of any integer Biginteger] hugeNumbers Biginteger 232323109292 3992"), new Bigintegert 43... PM 4 public static void main(String[] args { Page 501 13.3 Case Study: the Abstract... for numeric wrapper classes, Bigintege.. 5 matches System.out.println"o! isVfactorial(S0) 8 public static BigInteger factorialClong n) 9 BigInteger result BigInteger.ONE cotan 10 for (int i = 1; i

Explanation / Answer

BigDecimalFactorial.java

import java.math.BigDecimal;

import java.util.Scanner;

public class BigDecimalFactorial {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter the number: ");

int n = scan.nextInt();

BigDecimal bd = BigDecimal.valueOf(n);

System.out.println(n + "! = " + factorial(bd, bd));

}

static BigDecimal factorial(BigDecimal n, BigDecimal returnValue) {

if (n.equals(BigDecimal.ONE)) {

return returnValue;

}

BigDecimal lessOne = n.subtract(BigDecimal.ONE);

return factorial(lessOne, returnValue.multiply(lessOne));

}

}

Output:

Enter the number:
10
10! = 3628800

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote