Question

(Requires use of MATLAB)

Problem 1.3. (while loops, 2 points) Write a function my fibonacci (n), which prompts all elements of the fi bonacci sequence to the screen which are smaller than or equal n. Base your code on a while loop. Test your code with n 0, n 1, n 5 and n 25.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE FOR FIBNOCCI SERIES

int main()
{
int n;
long int i;
long int myfibnocci(int n);
printf("Enter the the value of n\n");
scanf("%d",&n);
printf("The terms of the Fibonacci series are :-\n",n);
myfibnocci(n);

return 0;
}

long int myfibnocci(n)
{
int first = 0, second = 1, next=1;
if(first<=n)
printf("%d\n",first);
if(second<=n)
printf("%d\n",second);
while(next<=n)
{
next = first + second;
if(next>n)
break;
else
printf("%d\n",next);
first = second;
second = next;

}

}

nter the the value of n he terma of the Pihonacci series are rocess returned 0x execution time 3.985 s ress any key to contin

Add a comment
Know the answer?
Add Answer to:
(Requires use of MATLAB) Write a function myfibonacci (n), which prompts all elements of the fibonacci...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • this is using MATLAB 2. Fibonacci sequence: A Fibonacci sequence is composed of elements created by...

    this is using MATLAB 2. Fibonacci sequence: A Fibonacci sequence is composed of elements created by adding the two previous elements. The simplest Fibonacci sequence starts with 1,1 and proceeds as follows: 1, 1, 2, 3, 5, 8, 13, . However, a Fibonacci sequence can be created with any two starting numbers. Create a MATLAB function called FL_fib_seq' (where F and L are your first and last initials) that creates a Fibonacci sequence. There should be three inputs. The first...

  • USING MATLAB 7. A Fibonacci sequence is composed of elements created by adding the two previous...

    USING MATLAB 7. A Fibonacci sequence is composed of elements created by adding the two previous elements. The simplest Fibonacci sequence starts with 1, 1 and proceeds as follows: 1,1,2,3,5,8,13, So, if f(1)-1 and f(2) -1, then f(3)-2)+f(1) We can represent this pattern as f(x) - f(x-1)+f(x-2). A Fibonacci sequence can be created with any two numbers. Prompt the user to enter the first two numbers in a Fibonacci sequence and the total number of elements requested in the sequence....

  • MATLAB 1. The Fibonacci sequence is defined by the recurrence relation Fn = Fn-1+Fn-2 where Fo...

    MATLAB 1. The Fibonacci sequence is defined by the recurrence relation Fn = Fn-1+Fn-2 where Fo = 0 and F1 = 1. Hence F2 = 1, F3 = 2, F4 = 3, etc. In this problem you will use three different methods to compute the n-th element of the sequence. Then, you will compare the time complexity of these methods. (a) Write a recursive function called fibRec with the following declaration line begin code function nElem = fibrec (n) end...

  • write matlab script 5. Use a for loop to sum the elements in x = [1...

    write matlab script 5. Use a for loop to sum the elements in x = [1 2 3 4 5] Check your answer with sum function 6. Use a while loop to sum the elements in x = [1 2 3 4 5] Check your answer with sum function

  • I need this MATLAB question answered ASAP!!! thanks in advance! 10 Write a program which prompts...

    I need this MATLAB question answered ASAP!!! thanks in advance! 10 Write a program which prompts the user for data and returns the mean. The code should execute the following steps: 1) Prompts the user to enter the number of observations 2) Use a FOR-LOOP to get data from user. At each iteration, the user is asked for value of the i-th observation 3) At each iteration, print the mean of the sample up to and including the i-th observation...

  • Flowcharts & for loops 1. Using a for loop, write a MatLab script to convert ounces...

    Flowcharts & for loops 1. Using a for loop, write a MatLab script to convert ounces to grams for 0 to 16 ounces, in 1-ounce increments. Present output in a table; label columns appropriately. 2. A Fibonacci sequence is composed of elements created by adding the previous 2 elements. The simplest Fibonacci sequence starts with 1, 1 and proceeds as follows: 1,1,2,3,5,8… Element 3 is the sum of elements 1 and 2 (1+1=2) Element 4 is the sum of elements...

  • This basic problem demonstrates the while-loop. (USE MATLAB) Write a Matlab function called steps that takes one scalar...

    This basic problem demonstrates the while-loop. (USE MATLAB) Write a Matlab function called steps that takes one scalar as an input argument and returns two scalars as output arguments. If it is called this way [n, d_left] = steps(d), then it sets n equal to the minimum number of steps required to move from a distance of one foot away from a wall to a distance less than d feet from the wall. The first step is 1/2 foot. If...

  • Write a script in matlab that prompts the user to input the day, month, and year;...

    Write a script in matlab that prompts the user to input the day, month, and year; determine the day of the year (the number of days including the current day). Be sure to take leap year into account. Use a for loop Test your code on the following dates: February 28, 2015 April 9, 1976 October 12, 1887 Create a script that performs the same function as repmat. The only built-in commands you may use are input, size, and disp.

  • I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a...

    I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a program that uses a for loop to calculate a Fibonacci sequence (NOT RECURSIVE!!!) up to a given position, starting with position 0. This function defines a Fibonacci sequence: If the number is 0, the function returns a 0 If the number is 1, the function returns a 1 If the number is higher than 1, it returns the sum of the previous two numbers...

  • MATLAB Write a function called next_prime that takes a scalar positive integer input n. Use a whi...

    MATLAB Write a function called next_prime that takes a scalar positive integer input n. Use a while-loop to find and return k, the smallest prime number that is greater than n. Feel free to use the built-in isprime function (do not use nextprime). MATLAB

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT