Skip to content
Related Articles

Related Articles

Algorithms Quiz | Sudo Placement [1.5] | Question 7

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Aug, 2018

Below is the incomplete code for printing Fibonacci sequence. Which statement could be inserted at "code to be inserted" statement, such that code will run perfectly?




// CPP Program to print Fibonacci sequence
// using 2 variables
#include <iostream>
using std::cout;
void fib(int n)
{
    int a = 0, b = 1;
    if (n >= 0)
        cout << a << " ";
    if (n >= 1)
        cout << b << " ";
    for (int i = 2; i <= n; i++) {
        cout << a + b << " ";
        // code to be inserted here
  
    }
}
   
// Driver code
int main()
{
    fib(9);
    return 0;
}


(A) b = a – b; b = b + a;
(B) b = a + b; a = a – b;

(C) b = a + b; a = b – a;
(D) None of these.


Answer: (C)

Explanation: Refer : https://www.geeksforgeeks.org/print-fibonacci-sequence-using-2-variables/

Quiz of this Question

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!