Question

In: Computer Science

Write the code (in a function) that allows the user to display the first “nb” of...

Write the code (in a function) that allows the user to display the first “nb” of elements in the Fibonacci sequence (each value on a separate line in the console).

The order of the Fibonacci sequence goes as follows: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 and on to infinity. Each number is the sum of the previous two (with the 2 starting numbers being 1 and 1). This series of numbers is known as the Fibonacci numbers or the Fibonacci sequence.

The function name should be “fibonacci” and it should take an integer as a parameter (argument), i.e. “fibonacci(int nb)”.

When called the function should respond as follows:

Function call:

fibonacci(1);

Console result:

1

Function call:

fibonacci(2);

Console result:

1

1

Function call:

fibonacci(3);

Console result:

1

1

2

And so on, …

Solutions

Expert Solution

ANS:

#include<string>
#include<iostream>
using namespace std;

void fibonacci(int nb)
{
   cout<<"PROGRAM OF FIBONACCI"<<endl;
   int x=1;
   int x2=1;
   int next=0;
   for(int i=1;i<=nb;i++)
   {
       if(i==1)
       {
           cout<<x<<endl;
           continue;
       }
      
       if(i==2)
       {
           cout<<x2<<endl;
           continue;
          
       }
      
       next=x+x2;
       x=x2;
       x2=next;
      
       cout<<next<<endl;
      
   }
}

int main()
{
   cout<<"NB=3"<<endl;
   fibonacci(3);
   cout<<endl<<endl<<"NB=2"<<endl;
       fibonacci(2);
  
}

Comment down for any queries
Please give a thumbs up if you are satisfied with answer :)


Related Solutions

Write a program that prompts the user for their first and lastname. Display the first...
Write a program that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user.Ask the user to input a phone number.The program checks which part of Colorado a phone number is from using the values below.If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none of the digits...
Write the pseudocode that prompts the user for their first and last name. Display the first...
Write the pseudocode that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user. Ask the user to input a phone number. The program checks which part of Colorado a phone number is from using the values below. If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none...
Write out code for a nested if statement that allows a user to enter in a...
Write out code for a nested if statement that allows a user to enter in a product name, store the product into a variable called product name and checks to see if that product exists in your nested if statement. You must include 5 product names to search for. If it is then assign the price of the item to a variable called amount and then print the product name and the cost of the product to the console. If...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters a number, the program should print out the average of the last 3 numbers (or all numbers if 3 or less have been entered). I would like a detailed explanation so that a beginner level Java programmer could understand.
q1) using Matlab make a code that allows a user to enter a function like fractional...
q1) using Matlab make a code that allows a user to enter a function like fractional function and cubic liner function then the Matlab send a domain, range, continuity
q1) using Matlab make a code that allows a user to enter a function like fractional...
q1) using Matlab make a code that allows a user to enter a function like fractional function and cubic liner function then the Matlab send a domain, range, continuity
Please code C# 10. Write a program that allows a user to input names and corresponding...
Please code C# 10. Write a program that allows a user to input names and corresponding heights (assumed to be in inches). The user can enter an indefinite number of names and heights. After each entry, prompt the user whether they want to continue. If the user enters true, ask for the next name and height. If the user enters false, display the name of the tallest individual and their height. Sample run: “Name?” James “Height?” 50 “Continue?” True “Name?”...
3. Design a digital watch that allows the user to set and display time.
3. Design a digital watch that allows the user to set and display time.
You have been asked to write program that allows the user to input a first name,...
You have been asked to write program that allows the user to input a first name, middle initial (without the period), and last name of a user and then display that person’s name with the first, middle initial followed by a period, and last name last. BEFORE creating the program, use structured programming principles to document how you are going to develop the program. Use Microsoft Word to complete this part of the assessment. Answer each of the following areas:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT