Question

In: Computer Science

Let’s revisit the Fibonacci sequence, this time without an array. Recall that the sequence is defined...

Let’s revisit the Fibonacci sequence, this time without an array. Recall that the sequence is defined by the following recurrence relation: F0 = 0 F1 = 1 Fk = Fk-1 + Fk-2 So, the sequence looks like: 0, 1, 1, 2, 3, 5, 8, 13, 21, … Write a program fib.c consisting only of a main() function that prompts the user for the last Fibonacci number n to print, and then prints them from F0 to Fn. (No file input needed.) For instance, if the user enters 4, the output should be: F0: 0 F1: 1 F2: 1 F3: 2 F4: 3 Again, no arrays or pointers. For full credit use no more than five integer variables (and no other variables). You may assume that n > 1.

Solutions

Expert Solution

Solution:-

C Code:-

#include<stdio.h>
int main()
{
// Declaratio of variables
int i, term, z, x = 0, y = 1;
// Prompt user to Enter number of terms
printf("Enter the number of terms:");
scanf("%d",&term);
// Printing first two term results
printf("F0: %d F1: %d ",x,y);
// Looping from second term till user entered term
for(i = 2 ; i <= term ; i++ )
{
// Computing the next term
z = x + y;
// Printing the next term
printf("F%d: %d ",i,z);
// Swapping the term values
x = y;
y = z;
}
return 0;
}

Code snapshot:-

Output snapshot:-


Related Solutions

(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence,...
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 114, … etc. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. We define Fib(0)=0,...
The Fibonacci sequence is defined as F_1 = 1, F_2 = 1, and F_n = F_n-1...
The Fibonacci sequence is defined as F_1 = 1, F_2 = 1, and F_n = F_n-1 + F_n-2 for n >= 3. Calculate the sum F_1 + F_2 + ... + F_n using the fundamental theorem of summation.  
how to find arithmetic sequence, geometric sequence, and fibonacci sequence on excel?
how to find arithmetic sequence, geometric sequence, and fibonacci sequence on excel?
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc. Do not use zero-based counting; fibonacci(4)is 3, not 5. Your assignment...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics...
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics and applications to arrangement of flower petals, population growth of rabbits, and genetics. For each natural number n ≥ 1, the nth Fibonacci number fn is defined inductively by f1 = 1, f2 = 2, and fn+2 = fn+1 + fn (a) Compute the first 8 Fibonacci numbers f1, · · · , f8. (b) Show that for all natural numbers n, if α...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem solved with recursion. However, this can also be done more efficiently using dynamic programming. Create a program that uses dynamic programming techniques to list the first 30 Fibonacci numbers.
0.3 The Fibonacci numbers Fn are defined by F1 = 1, F2 = 1 and for...
0.3 The Fibonacci numbers Fn are defined by F1 = 1, F2 = 1 and for n >2, Fn = F sub (n-1) + F sub (n-2). Find a formula for Fn by solving the difference equation.
Google the first 50 numbers of the Fibonacci sequence (starting with 1) to answer the following...
Google the first 50 numbers of the Fibonacci sequence (starting with 1) to answer the following questions:          (a) Test to see if the leading digits conform to Benford’s law. Do this both graphically and analytically.          (b) Using the first 10 odd numbers in the sequence as sample 1 and the first 10 even numbers in the sequence as sample 2, use Wilcoxon’s Rank-Sum to test the claim that the numbers come from different populations.          (c) Repeat (b)...
For the Fibonacci sequence, f0 = f1 = 1 and fn+1 = fn + fn−1 for...
For the Fibonacci sequence, f0 = f1 = 1 and fn+1 = fn + fn−1 for all n > 1. Prove using induction: fn+1fn−1 − f2n = (−1)n.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT