Question

In: Computer Science

In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers...

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

  • Your task will be to write a Java program that will take an integer number N as input where 0<= N <= 70 and will calculate all the Fibonacci numbers from 0 to N.
  • You will store these Fibonacci numbers in an integer array of size N.
  • Finally, you will print all of the first N Fibonacci numbers.
  • Your Fibonacci calculation should be done by a separate method.
    • Your main method will just take the input and pass it to this method.
  • If N=0, the output will be 0.
  • If N < 0, you will ask the user for another correct input (between 0 and 70, inclusive).
    • You can use your previous code for this.

Hint:

You will use For loop along with an Integer/Long array to solve this problem. Do NOT use recursion!

Goals

  • Learn about Array creation and populating an array with data.
  • Learn about accessing data in an array using a for loop.

Sample Run

-2

75

5

0 1 1 2 3

Solutions

Expert Solution

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);
                }
                
        }

}

Related Solutions

(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence,...
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 114, … etc. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. We define Fib(0)=0,...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem solved with recursion. However, this can also be done more efficiently using dynamic programming. Create a program that uses dynamic programming techniques to list the first 30 Fibonacci numbers.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.(java language)
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax. in java
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
Write a loop to print a list of numbers starting at 64 and ending at 339....
Write a loop to print a list of numbers starting at 64 and ending at 339. Justify your syntax.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT