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 1
Input: n = 7
Output: 0 1 1 2 3 5 8
Program to print first ‘n’ Fibonacci Numbers using recursion:
Below is the idea to solve the problem:
Use recursion to find nth fibonacci number by calling for n-1 and n-2 and adding their return value. The base case will be if n=0 or n=1 then the fibonacci number will be 0 and 1 respectively.
Follow the below steps to Implement the idea:
- Build a recursive function that takes integer N as a parameter.
- If N = 0 fibonacci number will be 0.
- Else if n = 1 Fibonacci number will be 1.
- Else return value for func(n-1) + func(n-2).
Below is the Implementation of the above approach:
C++
#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;
}
|
Java
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)+ " " );
}
}
}
|
Python3
def fibonacci_numbers(num):
if num = = 0 :
return 0
elif num = = 1 :
return 1
else :
return fibonacci_numbers(num - 2 ) + fibonacci_numbers(num - 1 )
n = 7
for i in range ( 0 , n):
print (fibonacci_numbers(i), end = " " )
|
C#
using System;
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++){
Console.Write(fibonacci_numbers(i)+ " " );
}
}
}
|
Javascript
<script>
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) + " " );
}
</script>
|
PHP
<?php
function fibonacci_numbers( $num )
{
if ( $num == 0){
return 0;
}
elseif ( $num == 1){
return 1;
}
else {
return (fibonacci_numbers( $num -2)+fibonacci_numbers( $num -1));
}
}
$num =7;
for ( $i = 0; $i < $num ; $i ++){
echo fibonacci_numbers( $i );
echo " " ;
}
?>
|
Time Complexity: O(n*2n)
Auxiliary Space: O(n), For recursion call stack.
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++
#include <bits/stdc++.h>
using namespace std;
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;
}
}
int main()
{
printFibonacciNumbers(7);
return 0;
}
|
C
#include <stdio.h>
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;
}
}
int main()
{
printFibonacciNumbers(7);
return 0;
}
|
Java
class Test {
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;
}
}
public static void main(String[] args)
{
printFibonacciNumbers( 7 );
}
}
|
Python3
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
printFibonacciNumbers( 7 )
|
C#
using System;
class Test {
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;
}
}
public static void Main() { printFibonacciNumbers(7); }
}
|
Javascript
<script>
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;
}
}
printFibonacciNumbers(7);
</script>
|
PHP
<?php
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 ;
}
}
printFibonacciNumbers(7);
?>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
29 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...