Print Fibonacci sequence using 2 variables - GeeksforGeeks

26 Nov, 2021
Print the Fibonacci sequence. The first Fibonacci numbers are:  
 
C++
Java
Python3
C#
PHP
Javascript
// Simple CPP Program to print Fibonacci// sequence#include <iostream>using std::cout;void fib(int n){    int a = 0, b = 1, c;    if (n >= 0)        cout << a << " ";    if (n >= 1)        cout << b << " ";    for (int i = 2; i <= n; i++) {        c = a + b;        cout << c << " ";        a = b;        b = c;    }} // Driver codeint main(){    fib(9);    return 0;}
// Simple Java Program to print// Fibonacci  sequenceimport java.io.*;class GFG { static void fib(int n){    int a = 0, b = 1, c;    if (n >= 0)        System.out.print( a + " ");    if (n >= 1)        System.out.print( b + " ");    for (int i = 2; i <= n; i++)    {        c = a + b;        System.out.print( c + " ");        a = b;        b = c;    }}     // Driver code    public static void main (String[] args)    {            fib(9);    }} // This code is contributed by anuj_67.
# Simple Python3 Program to# print Fibonacci sequence def fib(n):    a = 0    b = 1    if (n >= 0):        print(a, end=' ')    if (n >= 1):        print(b, end=' ')    for i in range(2, n+1):        c = a + b        print(c, end=' ')        a = b        b = c # Driver codefib(9) # This code is contributed# by Prasad Kshirsagar
// Simple C# Program to print// Fibonacci sequenceusing System;class GFG{static void fib(int n){    int a = 0, b = 1, c;    if (n >= 0)        Console.Write( a + " ");    if (n >= 1)        Console.Write( b + " ");    for (int i = 2; i <= n; i++)    {        c = a + b;        Console.Write( c + " ");        a = b;        b = c;    }} // Driver codepublic static void Main (){        fib(9);}} // This code is contributed by anuj_67.
<?php// Simple php Program to print// Fibonacci sequence function fib( $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.?>
<script>    // Simple Javascript Program to print Fibonacci sequence         function fib(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>
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++
Java
Python3
C#
PHP
Javascript
// 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 << " ";        b = a + b;        a = b - a;    }} // Driver codeint main(){    fib(9);    return 0;}
// Java Program to print// Fibonacci sequence// using 2 variablesimport java.io.*;class GFG {     static void fib(int n){    int a = 0, b = 1;    if (n >= 0)        System.out.print(a + " ");    if (n >= 1)        System.out.print(b + " ");    for (int i = 2; i <= n; i++)    {        System.out.print(a + b + " ");        b = a + b;        a = b - a;    }}     // Driver code    public static void main (String[] args)    {        fib(9);    }} // This code is contributed by anuj_67.
# Simple Python3 Program to# print Fibonacci sequence# using 2 variables def fib(n):    a = 0    b = 1    if (n >= 0):        print(a, end=' ')    if (n >= 1):        print(b, end=' ')    for i in range(2, n+1):        print(a + b, end=' ')        b = a + b        a = b - a # Driver codefib(9) # This code is contributed by# Prasad Kshirsagar
// C# Program to print Fibonacci// sequence using 2 variablesusing System; class GFG {             static void fib(int n)    {        int a = 0, b = 1;                 if (n >= 0)            Console.Write(a + " ");        if (n >= 1)            Console.Write(b + " ");        for (int i = 2; i <= n; i++)        {            Console.Write(a + b + " ");            b = a + b;            a = b - a;        }    }     // Driver code    public static void Main ()    {        fib(9);    }} // This code is contributed by anuj_67.
<?php// PHP Program to print Fibonacci sequence// using 2 variables function fib( $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?>
<script>    // Javascript Program to print Fibonacci sequence using 2 variables         function fib(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>
0 1 1 2 3 5 8 13 21 34
 
Time Complexity: O(n)
Auxiliary Space: O(1)
vt_m
Prasad_Kshirsagar
divyeshrabadiya07
decode2207
gulshankumarar231
samim2000
Fibonacci
programming-puzzle
Dynamic Programming
Dynamic Programming
Fibonacci
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Longest Palindromic Substring | Set 1
Subset Sum Problem | DP-25
Matrix Chain Multiplication | DP-8
Coin Change | DP-7
Overlapping Subproblems Property in Dynamic Programming | DP-1
Sieve of Eratosthenes
Edit Distance | DP-5