Following is simple program to print first n Fibonacci numbers.

Examples :
Input : n = 3 Output : 1 1 2 Input : n = 7 Output : 1 1 2 3 5 8 13
C++
// C++ program to print first n Fibonacci numbers #include <bits/stdc++.h> using namespace std; // Function to print first n Fibonacci Numbers void printFibonacciNumbers(int n) { int f1 = 0, f2 = 1, i; if (n < 1) return; for (i = 1; i <= n; i++) { cout<<f2<<" "; int next = f1 + f2; f1 = f2; f2 = next; } } // Driver Code int main() { printFibonacciNumbers(7); return 0; } //This code is contributed by rathbhupendra |
chevron_right
filter_none
C
// C program to print first n Fibonacci numbers #include <stdio.h> // Function to print first n Fibonacci Numbers void printFibonacciNumbers(int n) { int f1 = 0, f2 = 1, i; if (n < 1) return; for (i = 1; i <= n; i++) { printf("%d ", f2); int next = f1 + f2; f1 = f2; f2 = next; } } // Driver Code int main() { printFibonacciNumbers(7); return 0; } |
chevron_right
filter_none
Java
// 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; 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); } } |
chevron_right
filter_none
Python3
# 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 for x in range(0, n): print(f2, end = " ") next = f1 + f2 f1 = f2 f2 = next # Driven code printFibonacciNumbers(7) # This code is contributed by Danish Raza |
chevron_right
filter_none
C#
// C# program to print first n Fibonacci Numbers using 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; 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. |
chevron_right
filter_none
PHP
<?php // PHP program to print first // n Fibonacci numbers // Function to print first n // Fibonacci Numbers function printFibonacciNumbers($n) { $f1 = 0; $f2 = 1; $i; if ($n < 1) return; 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 ?> |
chevron_right
filter_none
Output:
1 1 2 3 5 8 13
Time Complexity : O(n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geekforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print first n Fibonacci Numbers using direct formula
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Find the sum of first N odd Fibonacci numbers
- Program to print Fibonacci Triangle
- C++ Program to print Fibonacci Series using Class template
- C Program to print Fibonacci Series without using loop
- Program to print first N Prime numbers
- Program for Fibonacci numbers
- C Program for Fibonacci numbers
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Find n terms of Fibonacci type series with given first two terms
- Print Fibonacci Series in reverse order
- Print Fibonacci sequence using 2 variables
- 3 Different ways to print Fibonacci series in Java
- Print squares of first n natural numbers without using *, / and -
- Print all increasing sequences of length k from first n natural numbers
- Print first N Mosaic numbers
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Sum of Fibonacci Numbers

