Sarah is learning about the Fibonacci sequence in her maths class. Her teacher asked to start the sequence with two initial values, which are 0 & 1. Sarah is fascinated by this sequence and wants to explore it further.Write a java program that generates the first n numbers in the Fibonacci sequence in Recursive(with using recursion) and Non-recursive way(without using recursion), starting with the initial values of 0 and 1, and then prints them out.Sample Test Case:Enter the range: 10Fibonacci Sequence (Recursive):0 1 1 2 3 5 8 13 21 34 Fibonacci Sequence (Non-Recursive):0 1 1 2 3 5 8 13 21 34 Sample Test CasesTest Case 1:Expected Output:Enter·the·range:·5Fibonacci·Sequence·(Recursive):0·1·1·2·3·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·3·Test Case 2:Expected Output:Enter·the·range:·10Fibonacci·Sequence·(Recursive):0·1·1·2·3·5·8·13·21·34·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·3·5·8·13·21·34·Test Case 3:Expected Output:Enter·the·range:·4Fibonacci·Sequence·(Recursive):0·1·1·2·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·Test Case 4:Expected Output:Enter·the·range:·9Fibonacci·Sequence·(Recursive):0·1·1·2·3·5·8·13·21·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·3·5·8·13·21·
Question
Sarah is learning about the Fibonacci sequence in her maths class. Her teacher asked to start the sequence with two initial values, which are 0 & 1. Sarah is fascinated by this sequence and wants to explore it further.Write a java program that generates the first n numbers in the Fibonacci sequence in Recursive(with using recursion) and Non-recursive way(without using recursion), starting with the initial values of 0 and 1, and then prints them out.Sample Test Case:Enter the range: 10Fibonacci Sequence (Recursive):0 1 1 2 3 5 8 13 21 34 Fibonacci Sequence (Non-Recursive):0 1 1 2 3 5 8 13 21 34 Sample Test CasesTest Case 1:Expected Output:Enter·the·range:·5Fibonacci·Sequence·(Recursive):0·1·1·2·3·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·3·Test Case 2:Expected Output:Enter·the·range:·10Fibonacci·Sequence·(Recursive):0·1·1·2·3·5·8·13·21·34·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·3·5·8·13·21·34·Test Case 3:Expected Output:Enter·the·range:·4Fibonacci·Sequence·(Recursive):0·1·1·2·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·Test Case 4:Expected Output:Enter·the·range:·9Fibonacci·Sequence·(Recursive):0·1·1·2·3·5·8·13·21·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·3·5·8·13·21·
Solution
Sure, here is the Java program that generates the first n numbers in the Fibonacci sequence in both recursive and non-recursive ways:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the range: ");
int n = scanner.nextInt();
System.out.println("Fibonacci Sequence (Recursive):");
for (int i = 0; i < n; i++) {
System.out.print(fibonacciRecursive(i) + " ");
}
System.out.println("\nFibonacci Sequence (Non-Recursive):");
fibonacciNonRecursive(n);
}
public static int fibonacciRecursive(int n) {
if (n <= 1) {
return n;
} else {
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}
}
public static void fibonacciNonRecursive(int n) {
int a = 0, b = 1, c;
if (n >= 1)
System.out.print(a + " ");
if (n >= 2)
System.out.print(b + " ");
for (int i = 2; i < n; i++) {
c = a + b;
System.out.print(c + " ");
a = b;
b = c;
}
}
}
This program first asks the user to enter the range of the Fibonacci sequence. Then it prints the Fibonacci sequence up to the entered range in both recursive and non-recursive ways. The fibonacciRecursive method calculates the Fibonacci sequence in a recursive way, and the fibonacciNonRecursive method calculates it in a non-recursive way.
Similar Questions
Sarah is learning about the Fibonacci sequence in her maths class. Her teacher asked to start the sequence with two initial values, which are 0 & 1. Sarah is fascinated by this sequence and wants to explore it further.Write a java program that generates the first n numbers in the Fibonacci sequence in Recursive(with using recursion) and Non-recursive way(without using recursion), starting with the initial values of 0 and 1, and then prints them out.Sample Test Case:Enter the range: 10Fibonacci Sequence (Recursive):0 1 1 2 3 5 8 13 21 34 Fibonacci Sequence (Non-Recursive):0 1 1 2 3 5 8 13 21 34 Sample Test CasesTest Case 1:Expected Output:Enter·the·range:·5Fibonacci·Sequence·(Recursive):0·1·1·2·3·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·3·Test Case 2:Expected Output:Enter·the·range:·10Fibonacci·Sequence·(Recursive):0·1·1·2·3·5·8·13·21·34·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·3·5·8·13·21·34·Test Case 3:Expected Output:Enter·the·range:·4Fibonacci·Sequence·(Recursive):0·1·1·2·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·Test Case 4:Expected Output:Enter·the·range:·9Fibonacci·Sequence·(Recursive):0·1·1·2·3·5·8·13·21·Fibonacci·Sequence·(Non-Recursive):0·1·1·2·3·5·8·13·21·
Suppose the first fibonnaci number is 0 and the second is 1. What is the sixth fibonnaci number? ans. 6 8 5 7
A Fibonacci sequence is a sequence of numbers (called Fibonacci numbers)in which each number is the sum of the two preceding ones as following:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, .
Write a program in any programming language of your choice to prompt the user to define the starting value of a Fibonacci sequence, i.e., 0 or 1. The program is expected to prompt the user to specify a finite number of Fibonacci sequences to be generated and lastly print out the output. Zip the code together with a report explaining the code and a snapshot of the output and upload on SAKAI taking note of the submission deadline.
Problem StatementCreate a program that generates and prints the Fibonacci series up to a specified number 'N'. The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding numbers, starting with 0 and 1. Your program should take an integer input 'N' and display the Fibonacci series up to the Nth term using a while loop.Fibonacci series: 0, 1, 1, 2, 3, 5, 8,... Note: This question is one of the most asked questions in placements.Input format :The input consists of a positive integer N.Output format :The output displays the Fibonacci series up to the Nth term separated by space.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ N ≤ 20The Fibonacci series starts from 0.Sample test cases :Input 1 :7Output 1 :0 1 1 2 3 5 8 Input 2 :1Output 2 :0 Input 3 :20Output 3 :0 1 1 2 3 5 8 13 21 34 55
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.