An iterative approach to print first ‘n’ Fibonacci numbers
Below is the idea to solve the problem
- Use two variables f1 and f2 and initialize with 0 and 1 respectively because the 1st and 2nd elements of the Fibonacci series are 0 and 1 respectively.
- Iterate from 1 to n-1 and print f2 then store f2 in temp variable and update f2 with f2 + f1 and f1 as f2.
Below is the Implementation of the above approach:
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 ; cout << f1 << " " ; 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 |
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 ; printf ( "%d " , f1); 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; } |
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; System.out.print(f1 + " " ); 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 ); } } |
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 print (f1, end = " " ) for x in range ( 1 , n): print (f2, end = " " ) next = f1 + f2 f1 = f2 f2 = next # Driven code printFibonacciNumbers( 7 ) # This code is contributed by Danish Raza |
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 ; 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. |
Javascript
<script> // Javascript program to print // first n Fibonacci numbers // Function to print // first n Fibonacci Numbers function 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> |
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 ; 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 ?> |
Output
0 1 1 2 3 5 8
Time Complexity: O(n)
Auxiliary Space: O(1)
Program to print first n Fibonacci Numbers | Set 1
Given an integer N. The task is to find the first N Fibonacci numbers.
Examples :
Input: n = 3
Output: 0 1 1Input: n = 7
Output: 0 1 1 2 3 5 8