Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please use comments for each code block and read the instructions and hints carefully. Question 2 of 2. Fibonacci Sequence: [50 marks] Write a C++

Please use comments for each code block and read the instructions and hints carefully.

image text in transcribed

image text in transcribed

Question 2 of 2. Fibonacci Sequence: [50 marks] Write a C++ program which will ask the user to input an integer number N. Your program should print the first N numbers in the Fibonacci sequence. Fibonacci sequence are a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1 F_(0)=0, F_(1)= 1 F_(n) =F_(n-1) +F_{n-2) The beginning of the sequence would then be 0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... We do not need arrays for this problem. We need loops. We can display this sequence each number at a time, by computing the sum of the previous two numbers in each iteration of the loop. The old previous numbers are then updated to include the new sum we just computed. This new sum and the previous number can now be used to print the next number in the Fibonacci sequence. Given a user input number N, print the first N numbers in the series. Print the first 6 numbers in the Fibonacci sequence. Example: Input: N= 6. Output: 0, 1, 1, 2, 3, 5 Print the first 8 numbers in the Fibonacci sequence. Example: Input: N= 8. Output: 0,1, 1, 2, 3, 5, 8, 13 Print the first 15 numbers in the Fibonacci sequence. Example: Input: N=15. Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 Print the first 25 numbers in the Fibonacci sequence. Example: Input: N = 25. Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610. 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368 Make sure your code works for any input number, not just the test cases. Your code will be tested on other test cases not listed here. Please properly comment your code before submission. Sample Test cases: Test case 1: Input: Enter Fibonacci numbers required: 8 Test case 2: Input: Enter Fibonacci numbers required: 17 Output: 0,1, 1, 2, 3, 5, 8, 13, Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, Test case 3: Input: Enter Fibonacci numbers required: 3 Test case 4: Input: Enter Fibonacci numbers required: 10 Output: 0, 1, 1, Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, HINTS: Think about the problem before you start coding. Keep a count of how many numbers have been printed so far. Stop your loop when N numbers are printed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions