Using Java. Programming Exercises 18.1 Chapter 18: Recursion Programming Exercis
ID: 3855313 • Letter: U
Question
Using Java. Programming Exercises 18.1 Chapter 18: Recursion Programming Exercises: 18.1 (Factorial) Using the Biginteger class introduced in section 10.9, you can find the factorial Programming Exercises: 18.7 (Fibonacci series) Modify Lisitng 18.2. ComputeFiboacci.java, so that the program finds the number of times the fib method is claaed. Programming Exercises: 18.36 (Sierpinski triangle) Write a program that lets the user to enter the order and display the filled Sierpinski triangle as shown in your textbook below figure 18.18. 18.36 (Sierpinski triangle) Write a program that lets the user to enter the order and display the filled Sierpinski triangles as shown in Figure 18.18. Exercise18 Exerase18 Enter an crde Enter an order Enter an order: ENter an order FIGURE 18.18 A filled Sierpinski triangle is displayedExplanation / Answer
import java.util.Scanner;
import java.math.*;
public class FindFactorial {
public static void main(String[] args) {
// scanner instance to read user input
Scanner sc = new Scanner(System.in);
System.out.print("Enter integer: ");
//reading number from user
String num = sc.nextLine();
// Display factorial
System.out.println("Factorial of " + num + " is "
+ calculateFactorial(new BigInteger(num)));
}
// calculate factorial and return result
public static BigInteger calculateFactorial(BigInteger num) {
if (num.equals(BigInteger.ZERO)) // if num is zero
return BigInteger.ONE;
else // otherwise recursively calling to calculate factorial
return num.multiply(factorial(num.subtract(BigInteger.ONE)));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.