Print Fibonacci sequence using 2 variables

Print the Fibonacci sequence. The first Fibonacci numbers are: 
 
C++
| // Simple CPP Program to print Fibonacci // sequence#include <iostream>usingstd::cout;voidfib(intn){    inta = 0, b = 1, c;    if(n >= 0)        cout << a << " ";    if(n >= 1)        cout << b << " ";    for(inti = 2; i <= n; i++) {        c = a + b;        cout << c << " ";        a = b;        b = c;    }}// Driver codeintmain(){    fib(9);    return0;} | 
Java
| // Simple Java Program to print// Fibonacci  sequenceimportjava.io.*;classGFG {staticvoidfib(intn){    inta = 0, b = 1, c;    if(n >= 0)        System.out.print( a + " ");    if(n >= 1)        System.out.print( b + " ");    for(inti = 2; i <= n; i++)     {        c = a + b;        System.out.print( c + " ");        a = b;        b = c;    }}    // Driver code    publicstaticvoidmain (String[] args)     {            fib(9);    }}// This code is contributed by anuj_67. | 
Python3
| # Simple Python3 Program to # print Fibonacci sequencedeffib(n):    a =0    b =1    if(n >=0):        print(a, end=' ')    if(n >=1):        print(b, end=' ')    fori inrange(2, n+1):        c =a +b        print(c, end=' ')        a =b        b =c# Driver codefib(9)# This code is contributed # by Prasad Kshirsagar | 
C#
| // Simple C# Program to print// Fibonacci sequenceusingSystem;classGFG {staticvoidfib(intn){    inta = 0, b = 1, c;    if(n >= 0)        Console.Write( a + " ");    if(n >= 1)        Console.Write( b + " ");    for(inti = 2; i <= n; i++)     {        c = a + b;        Console.Write( c + " ");        a = b;        b = c;    }}// Driver codepublicstaticvoidMain () {        fib(9);}}// This code is contributed by anuj_67. | 
PHP
| <?php// Simple php Program to print// Fibonacci sequencefunctionfib( $n){    $a= 0; $b= 1; $c;    if($n>= 0)        echo$a, " ";    if($n>= 1)        echo$b, " ";    for( $i= 2; $i<= $n; $i++)     {        $c= $a+ $b;        echo$c, " ";        $a= $b;        $b= $c;    }}// Driver codefib(9);// This code is contributed by anuj_67.?> | 
Javascript
| <script>    // Simple Javascript Program to print Fibonacci sequence        functionfib(n)    {        let a = 0, b = 1, c;        if(n >= 0)            document.write( a + " ");        if(n >= 1)            document.write( b + " ");        for(let i = 2; i <= n; i++)         {            c = a + b;            document.write( c + " ");            a = b;            b = c;        }    }        fib(9);// This code is contributed by divyeshrabadiya07.</script> | 
Output: 
0 1 1 2 3 5 8 13 21 34
Time Complexity: O(n)
Auxiliary Space: O(1)
How to print using 2 variables instead of 3 variables?
The algorithm with 2 variables is: 
1. print a+b. 
2. add a to b. 
3. assign b – a to a. 
 
C++
| // CPP Program to print Fibonacci sequence// using 2 variables#include <iostream>usingstd::cout;voidfib(intn){    inta = 0, b = 1;    if(n >= 0)        cout << a << " ";    if(n >= 1)        cout << b << " ";    for(inti = 2; i <= n; i++) {        cout << a + b << " ";        b = a + b;        a = b - a;    }}// Driver codeintmain(){    fib(9);    return0;} | 
Java
| // Java Program to print // Fibonacci sequence// using 2 variablesimportjava.io.*;classGFG {    staticvoidfib(intn){    inta = 0, b = 1;    if(n >= 0)        System.out.print(a + " ");    if(n >= 1)        System.out.print(b + " ");    for(inti = 2; i <= n; i++)     {        System.out.print(a + b + " ");        b = a + b;        a = b - a;    }}    // Driver code    publicstaticvoidmain (String[] args)     {        fib(9);    }}// This code is contributed by anuj_67. | 
Python3
| # Simple Python3 Program to# print Fibonacci sequence# using 2 variablesdeffib(n):    a =0    b =1    if(n >=0):        print(a, end=' ')    if(n >=1):        print(b, end=' ')    fori inrange(2, n+1):        print(a +b, end=' ')        b =a +b        a =b -a# Driver codefib(9)# This code is contributed by# Prasad Kshirsagar | 
C#
| // C# Program to print Fibonacci// sequence using 2 variablesusingSystem;classGFG {            staticvoidfib(intn)    {        inta = 0, b = 1;                if(n >= 0)            Console.Write(a + " ");        if(n >= 1)            Console.Write(b + " ");        for(inti = 2; i <= n; i++)         {            Console.Write(a + b + " ");            b = a + b;            a = b - a;        }    }    // Driver code    publicstaticvoidMain ()     {        fib(9);    }}// This code is contributed by anuj_67. | 
PHP
| <?php// PHP Program to print Fibonacci sequence// using 2 variablesfunctionfib( $n){    $a= 0; $b= 1;    if($n>= 0)        echo$a, " ";    if($n>= 1)        echo$b, " ";    for($i= 2; $i<= $n; $i++)    {        echo$a+ $b, " ";        $b= $a+ $b;        $a= $b- $a;    }}    // Driver code    fib(9);    // This code is contributed by anuj_67?> | 
Javascript
| <script>    // Javascript Program to print Fibonacci sequence using 2 variables        functionfib(n)    {        let a = 0, b = 1;                 if(n >= 0)            document.write(a + " ");        if(n >= 1)            document.write(b + " ");        for(let i = 2; i <= n; i++)        {            document.write(a + b + " ");            b = a + b;            a = b - a;        }    }        fib(9);      // This code is contributed by decode2207.</script> | 
Output : 
0 1 1 2 3 5 8 13 21 34
Time Complexity: O(n)
Auxiliary Space: O(1)
                                            Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
 
				 
					



