Program to print first n Fibonacci Numbers | Set 1

07 Jul, 2022
Following is a simple program to print first n Fibonacci numbers. 
Examples : 
Input : n = 3
Output : 0 1 1

Input : n = 7
Output : 0 1 1 2 3 5 8
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to print// first n Fibonacci numbers#include <bits/stdc++.h>using namespace std; // Function to print// first n Fibonacci Numbersvoid printFibonacciNumbers(int n){    int f1 = 0, f2 = 1, i;     if (n < 1)        return;    cout << f1 << " ";    for (i = 1; i < n; i++) {        cout << f2 << " ";        int next = f1 + f2;        f1 = f2;        f2 = next;    }} // Driver Codeint main(){    printFibonacciNumbers(7);    return 0;} // This code is contributed by rathbhupendra
// C program to print// first n Fibonacci numbers#include <stdio.h> // Function to print// first n Fibonacci Numbersvoid printFibonacciNumbers(int n){    int f1 = 0, f2 = 1, i;     if (n < 1)        return;    printf("%d ", f1);    for (i = 1; i < n; i++) {        printf("%d ", f2);        int next = f1 + f2;        f1 = f2;        f2 = next;    }} // Driver Codeint main(){    printFibonacciNumbers(7);    return 0;}
// Java program to print// first n Fibonacci Numbers class Test {    // Method to print    // first n Fibonacci Numbers    static void printFibonacciNumbers(int n)    {        int f1 = 0, f2 = 1, i;         if (n < 1)            return;        System.out.print(f1 + " ");        for (i = 1; i < n; i++)        {            System.out.print(f2 + " ");            int next = f1 + f2;            f1 = f2;            f2 = next;        }    }     // Driver Code    public static void main(String[] args)    {        printFibonacciNumbers(7);    }}
# Python program to print first n# Fibonacci numbers # Function to print first n# Fibonacci Numbers  def printFibonacciNumbers(n):     f1 = 0    f2 = 1    if (n < 1):        return    print(f1, end=" ")    for x in range(1, n):        print(f2, end=" ")        next = f1 + f2        f1 = f2        f2 = next  # Driven codeprintFibonacciNumbers(7) # This code is contributed by Danish Raza
// C# program to print// first n Fibonacci Numbersusing System; class Test {    // Method to print    // first n Fibonacci Numbers    static void printFibonacciNumbers(int n)    {        int f1 = 0, f2 = 1, i;         if (n < 1)            return;        Console.Write(f1 + " ");        for (i = 1; i < n; i++) {            Console.Write(f2 + " ");            int next = f1 + f2;            f1 = f2;            f2 = next;        }    }     // Driver Code    public static void Main()    {         printFibonacciNumbers(7);    }} // This code is contributed by nitin mittal.
<?php// PHP program to print first// n Fibonacci numbers // Function to print first n// Fibonacci Numbersfunction printFibonacciNumbers($n){    $f1 = 0;    $f2 = 1;    $i;     if ($n < 1)        return;    echo($f1);    echo(" ");    for ($i = 1; $i < $n; $i++)    {        echo($f2);        echo(" ");        $next = $f1 + $f2;        $f1 = $f2;        $f2 = $next;    }}     // Driver Code    printFibonacciNumbers(7);     // This code is contributed by nitin mittal?>
<script> // Javascript program to print// first n Fibonacci numbers // Function to print// first n Fibonacci Numbersfunction printFibonacciNumbers(n){    let f1 = 0, f2 = 1, i;     if (n < 1)        return;    document.write(f1 + " ");    for (i = 1; i < n; i++) {        document.write(f2 + " ");        let next = f1 + f2;        f1 = f2;        f2 = next;    }} // Driver Code     printFibonacciNumbers(7);     // This code is contributed by Mayank Tyagi </script>
0 1 1 2 3 5 8 
C++
Java
Python3
C#
Javascript
#include <iostream>using namespace std; int fibonacci_numbers(int n){    if(n == 0){        return 0;    }    else if(n == 1){        return 1;    }    else{        return fibonacci_numbers(n-2) + fibonacci_numbers(n-1);    }} int main() {    int n = 7;      for(int i = 0; i < n; i++)    {        cout << fibonacci_numbers(i) << " ";    }    return 0;}// This code is contributed by Rupesh Kapse
/*package whatever //do not write package name here */import java.io.*; class GFG {  public static int fibonacci_numbers(int n)  {    if(n == 0){      return 0;    }    else if(n == 1){      return 1;    }    else{      return fibonacci_numbers(n-2) + fibonacci_numbers(n-1);    }  }  public static void main (String[] args) {    int n = 7;    for(int i = 0; i < n; i++){      System.out.print(fibonacci_numbers(i)+ " ");    }  }} // This code is contributed by Rupesh Kapse
# python code to print first n fibonacci numbers  def fibonacci_numbers(num):    if num == 0:        return 0    elif num == 1:        return 1    else:        # printing fibonacci numbers        return fibonacci_numbers(num-2)+fibonacci_numbers(num-1)  n = 7for i in range(0, n):    print(fibonacci_numbers(i), end=" ")    # this code is contributed by gangarajula laxmi
// C# code to implement the approachusing System; class GFG {     // Method to calculate the nth fibonacci number  public static int fibonacci_numbers(int n)  {    if(n == 0){      return 0;    }    else if(n == 1){      return 1;    }    else{      return fibonacci_numbers(n-2) + fibonacci_numbers(n-1);    }  }        // Driver Code  public static void Main (string[] args) {    int n = 7;    for(int i = 0; i < n; i++){      // Function call      Console.Write(fibonacci_numbers(i)+ " ");    }  }}  // This code is contributed by phasing17
<script>        // JavaScript code for the above approach          function fibonacci_numbers(n) {            if (n == 0) {                return 0;            }            else if (n == 1) {                return 1;            }            else {                return fibonacci_numbers(n - 2) + fibonacci_numbers(n - 1);            }        }          let n = 7;        for (let i = 0; i < n; i++) {            document.write(fibonacci_numbers(i) + " ");        }     // This code is contributed by Potta Lokesh    </script>
0 1 1 2 3 5 8 
Time Complexity: O(n)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geekforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Sahil_Chhabra
nitin mittal
rathbhupendra
kajalkukreja694
mayanktyagi1709
subham348
laxmigangarajula03
rupeshsk30
lokeshpotta20
phasing17
Fibonacci
Wipro
Mathematical
School Programming
Wipro
Mathematical
Fibonacci
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Sieve of Eratosthenes
Prime Numbers
Program to find GCD or HCF of two numbers
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java