In: Computer Science
In this assignment, you will calculate and print a list of
Fibonacci Numbers . Fibonacci numbers are the
numbers in the following integer sequence, called the
Fibonacci sequence, and characterized by the fact
that every number after the first two is the sum of the two
preceding ones:
The sequence Fn of Fibonacci numbers is defined by the recurrence relation: which says any Nth Fibonacci number is the sum of the (N-1) and (N-2)th Fibonacci numbers.
Instructions
Hint:
You will use For loop along with an Integer/Long array to solve this problem. Do NOT use recursion!
Goals
Sample Run
-2
75
5
0 1 1 2 3
package arrays;
import java.util.Scanner;
public class Fibonacci {
void fibo(int n) {
int f1;
int f2=0;
int f3=1;
int a[]=new int[n];
for (int i=0;i<n;i++) {
a[i]=f3;
f1=f2;
f2=f3;
f3=f1+f2;
}
System.out.print(0+" ");
for (int j=0;j<a.length-1;j++) {
System.out.print(a[j]+" ");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scn = new Scanner(System.in);
Fibonacci f = new Fibonacci();
int n=scn.nextInt();
if (n==0)
{
System.out.println(0);
}
else if (n<0){
System.out.println("enter correct input between 0 to 70 only");
int a=scn.nextInt();
f.fibo(a);
}
else {
f.fibo(n);
}
}
}