Programming Exercise 18.7 Please include specific comments Chapter 18: Recursion
ID: 3855315 • Letter: P
Question
Programming Exercise 18.7 Please include specific commentsChapter 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 displayed
Explanation / Answer
I have included my specific comments in the code, please go through it:
CODE:
import java.util.Scanner;
public class ComputeFibonacci
{
/**Main method */
pubic static void main(String[] args)
{
//creating a scanner
Scanner inpu = new Scanner (System.in);
//Enter the index value
System.out.print("Enter an index for a Fibanacci number);
//Reading the entered index value
int index = input.newInt();
//Find and display the Fibonacci number
System.out.println("The Fivonacci number at index" +index + "is" + fib(index));
}
/**the method for finding the Fibonacci number */
public static long fib(long index)
{
if (index ==0)//base case
return 0;
else if (index ==1)//Base case
return 1;
elase // reduction and recursive calls
return fib(index-1)+fib(index-2);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.