Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Fibonacci sequence is a pattern of integers starting with zero and one, wher

ID: 3752826 • Letter: T

Question

The Fibonacci sequence is a pattern of integers starting with zero and one, where each subsequent value is equal to the sum of the prior two values:
Fn = Fn-1 + Fn-2, where F0 = 0 and F1 = 1.
Create a Java program which will prompt the user with “How many numbers do you want to have in the sequence? (It must be greater than 2.)”. Then it will prompt the user with “Do you want to skip the odd numbers? 1=yes, 2=no”.
The program will then run according to the inputs from the user. That is, if the user enters ‘12’ and ‘2’, then the program will just print the first 12 numbers in the sequence. If the user enters ‘12’ and ‘1’, then the program will try to print the first 12 numbers but will replace the odd numbers with blanks as shown below.
Sample runs:

$java Fibonacci Fibonacci Sequence: How many numbers do you want to have in the sequence? (It must be greater than 2.) 12 Do you want to skip the odd numbers? 1-yes,2-no Sample 4 6 10 34 12

Explanation / Answer

ANSWER:-

import java.util.Scanner;

class Fibonacci

{

public static void main (String[] args)

{

int t1 = 0, t2 = 1, nextTerm;

Scanner reader = new Scanner(System.in); // Reading from System.in

System.out.println("how many numbers do you want to have in the sequence : ");

int n = reader.nextInt();

System.out.println("do you want to skip the odd numbers ? 1=yes ,2=no : ");

int m = reader.nextInt();

if(m==1)

{

for (int i = 1; i <= n; ++i)

{

if(t1%2==0)

System.out.println(i+" "+t1);

else

System.out.println(i);

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

}

}

else

{

for (int i = 1; i <= n; ++i)

{

System.out.println(i+" "+t1);

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

}

}

}

}

// OUTPUT

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote