Question

In: Computer Science

The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 … begins with the...

  1. The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 … begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. Write a non-recursive function Fibonacci (n) that calculates the nth Fibonacci number. Write a program to display a table of terms and the Fibonacci number in two columns for the first 15 terms, using the function you created.

Solutions

Expert Solution

Code:

#include<stdio.h>
int fibno(int n)
{
   int f1=0,f2=1,i;/*Declared variables*/
   if(n==0)
   {
       return 0;/*When n is 0 we return 0*/
   }
   else if(n==1)
   {
       return 1;/*When n is 1 we return 1*/
   }
   else
   {
       int sum;
       for(i=1;i<n;i++)
       {/*Here we calculate the fibnocci series*/
           sum=f1+f2;
           f1=f2;
           f2=sum;
       }
       return sum;
       /*Finally we return the number*/
      
   }
  
}
int main()
{
   int i=1,n=15;/*Here we declared the variables*/
   printf("First 15 Fibonacci series: \n");
   for(i=0;i<n;i++)
   {
       printf("%d\t%d\n",i+1,fibno(i));
       /*Here we call the function for every number*/
   }
}

Output:

indentation:


Related Solutions

The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13,...
The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 . . . See the pattern? Each element in the series is the sum of the preceding two elements. Here is a recursive formula for calculating the nth number of the sequence: Fib(N) = {N, if N = 0 or 1 Fib(N - 2) + Fib(N - 1), if N > 1 a) Write a recursive method fibonacci that returns...
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,.... Formally,...
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,.... Formally, it can be expressed as: fib0 = 0 fib1 = 1 fibn = fibn-1 + fibn-2 Write a multithreaded C++ program that generates the Fibonacci series using the pthread library. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program will generate. The program will then create a separate thread that will...
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5...
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5 3 0 2 5 4 1 6 9 5 3 1 2 10 6 1 1 2 1 19 6 6 6 7 0 4 1 1 1 0 1 9 2 2 2 1 16 10 10 5 2 3 1 4 4 4 3 6 2 8 5 2 7 1 6 4 0 3 1 1 1 Background: A group of...
Consider the following time series. t 1 2 3 4 5 yt 6 10 8 13...
Consider the following time series. t 1 2 3 4 5 yt 6 10 8 13 15 (a) Choose the correct time series plot. (i) (ii) (iii) (iv) What type of pattern exists in the data? (b) Use simple linear regression analysis to find the parameters for the line that minimizes MSE for this time series. If required, round your answers to two decimal places. y-intercept, b0 = 4.1 Slope, b1 = 2.1 MSE = ???? (c) What is the...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6 13 7 33 8 42 9 66 10 77 11 105 12 103 13 110 14 105 15 84 16 70 17 51 18 40 19 27 20 27 21 15 22 5 23 7 24 2 25 2 26 1 27 0 28 0 29 0 30 0 (7) On the Histogram worksheet, calculate all frequencies of the distribution using the table shown....
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8 −9 6) (1) Count the number of rows that contain negative components. (2) Obtain the inverse of A and count the number of columns that contain even number of positive components. (3) Assign column names (a,b,c,d) to the columns of A. (4) Transform the matrix A into a vector object a by stacking rows. (5) Replace the diagonal components of A with (0,0,2,3). Hint:...
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1...
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1 0 2 11 5 11 27 4 0 6 Create Standard Deviation Chart (Normal Distribution Curve)
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6...
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6 1 5 6 8 2 4 6 7 3 4 5 9 10 5 8 9 4 7 9 6 7 8 6 How can I detect when 'cin' starts reading from a new line. The amount of numbers in each row is unknown. I need them in type 'int' to use the data.
A= 1 2 4 0 1 -2 -1 0 1 2 0 3 8 1 4...
A= 1 2 4 0 1 -2 -1 0 1 2 0 3 8 1 4 . Let W denote the row space for A. (a) Find an orthonormal basis for W and for W⊥. (b) Compute projW⊥(1 1 1 1 1 ).
1. Assume you have the following cashflows Time: 0 1 2. 3 4 5 Cf: 21...
1. Assume you have the following cashflows Time: 0 1 2. 3 4 5 Cf: 21 4 5 6. 6(1 + .02). 6(1+.02)^2 After year 3 cashflows will grow by constant 2% rate What is the discounted present value of these cashflows if your project's cost of capital is 13%?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT