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

Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn =...
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn = Fn−1 + Fn−2 for n ≥ 2. Use induciton to prove that F0F1 + F1F2 + · · · + F2n−1 F2n = F^2 2n for all positive integer n.
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.  
(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,...
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2)...
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2) with f(0) = 0 and f(1) = 1. Find f(54) by a program or maually. Note that this number must be positive and f(53) = 53.......73 (starting with 53 and ending with 73). I must admit that my three machines including a desktop are unable to find f(54) and they quit during computation. The answer is f(54) = 86267571272 */ The Java code: public...
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 α...
The Lucas Numbers are a sequence very similar to the Fibonacci Sequence, the only difference being...
The Lucas Numbers are a sequence very similar to the Fibonacci Sequence, the only difference being that the Lucas Numbers start with L0 = 2 and L1 = 1 as opposed to Fibonacci’s F0 = 0 and F1 = 1. Concretely, they are defined by L0 = 2, L1 = 1, and Ln := Ln−1 + Ln−2 for n > 1. Write a function in C++ that takes an integer argument N and returns the sum of the first N...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT