Print the Fibonacci sequence . The first Fibonacci numbers are:
C++
#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;
}
}
int
main()
{
fib(9);
return
0;
}
Java
import
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;
}
}
public
static
void
main (String[] args)
{
fib(
9
);
}
}
Python3
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
fib(
9
)
C#
using
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;
}
}
public
static
void
Main ()
{
fib(9);
}
}
PHP
<?php
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
;
}
}
fib(9);
?>
Javascript
<script>
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);
</script>
Output:
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++
#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;
}
}
int
main()
{
fib(9);
return
0;
}
Java
import
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;
}
}
public
static
void
main (String[] args)
{
fib(
9
);
}
}
Python3
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
fib(
9
)
C#
using
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;
}
}
public
static
void
Main ()
{
fib(9);
}
}
PHP
<?php
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
;
}
}
fib(9);
?>
Javascript
<script>
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);
</script>
Output :
0 1 1 2 3 5 8 13 21 34
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!