Programming is closely related to puzzles and mathematics. Solving programming puzzles is a way to keep you mentally active and fit. It helps to build up problem-solving skills.
The Fibonacci Sequence problem is one of the logic-based programming problems that are fun to solve and also asked in technical interviews. We think it's an excellent project to hone your arithmetic skills in any language of your choosing.
Sounds good? Let's get started. In this article, you'll learn how to print a Fibonacci sequence up to n terms and n value.
What Is a Fibonacci Sequence?
A Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. In Mathematics, this sequence is denoted by Fn.
F<sub>0</sub> = 0 and F<sub>1</sub> = 1.
and
F<sub>n</sub> = F<sub>n-1</sub> + F<sub>n-2</sub>
Fibonacci Sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Printing the First n Fibonacci Numbers
Problem Statement
You're given a number n. You need to print the Fibonacci sequence up to the first n terms.
Example 1: Let n = 5.
First 5 Fibonacci Numbers: 0 1 1 2 3
Thus, the output is 0 1 1 2 3.
Example 2: Let n = 7.
First 7 Fibonacci Numbers: 0 1 1 2 3 5 8
Thus, the output is 0 1 1 2 3 5 8.
C++ Program to Print the First n Fibonacci Numbers
Below is the C++ program to print the first n Fibonacci numbers:
// C++ program to print the Fibonacci sequence upto n terms
#include <iostream>
using namespace std;
void printFibonacciSequence(int n)
{
int a = 0, b = 1;
int nextTerm;
if (n<1)
{
return;
}
cout << "Fibonacci Sequence Upto " << n << " terms:" << endl;
cout << a << " ";
for(int i=1; i<n; i++)
{
cout << b << " ";
// Next term is the sum of the last two terms
nextTerm = a + b;
a = b;
b = nextTerm;
}
cout << endl;
}
int main()
{
int n1 = 5;
printFibonacciSequence(n1);
int n2 = 7;
printFibonacciSequence(n2);
int n3 = 3;
printFibonacciSequence(n3);
int n4 = 10;
printFibonacciSequence(n4);
int n5 = 8;
printFibonacciSequence(n5);
return 0;
}
Output:
Fibonacci Sequence Upto 5 terms:
0 1 1 2 3
Fibonacci Sequence Upto 7 terms:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 3 terms:
0 1 1
Fibonacci Sequence Upto 10 terms:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 8 terms:
0 1 1 2 3 5 8 13
Python Program to Print the First n Fibonacci Numbers
Below is the Python program to print the first n Fibonacci numbers:
# Python program to print the fibonacci sequence upto n terms
def printFibonacciSequence(n):
a = 0
b = 1
if (n < 1):
return
print("Fibonacci Sequence Upto", n, "terms:")
print(a, end=" ")
for i in range(1, n):
print(b, end=" ")
# Next term is the sum of the last two terms
nextTerm = a + b
a = b
b = nextTerm
print()
n1 = 5
printFibonacciSequence(n1)
n2 = 7
printFibonacciSequence(n2)
n3 = 3
printFibonacciSequence(n3)
n4 = 10
printFibonacciSequence(n4)
n5 = 8
printFibonacciSequence(n5)
Output:
Fibonacci Sequence Upto 5 terms:
0 1 1 2 3
Fibonacci Sequence Upto 7 terms:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 3 terms:
0 1 1
Fibonacci Sequence Upto 10 terms:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 8 terms:
0 1 1 2 3 5 8 13
JavaScript Program to Print the First n Fibonacci Numbers
Below is the JavaScript program to print the first n Fibonacci numbers:
// JavaScript program to print the Fibonacci sequence up to n terms
function printFibonacciSequence(n) {
let a = 0, b = 1;
let nextTerm;
if (n<1) {
return;
}
document.write("Fibonacci Sequence Upto " + n + " terms:" + "
");
document.write(a + " ");
for(let i=1; i<n; i++) {
document.write(b + " ");
// Next term is the sum of the last two terms
nextTerm = a + b;
a = b;
b = nextTerm;
}
document.write("
");
}
let n1 = 5;
printFibonacciSequence(n1);
let n2 = 7;
printFibonacciSequence(n2);
let n3 = 3;
printFibonacciSequence(n3);
let n4 = 10;
printFibonacciSequence(n4);
let n5 = 8;
printFibonacciSequence(n5);
Output:
Fibonacci Sequence Upto 5 terms:
0 1 1 2 3
Fibonacci Sequence Upto 7 terms:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 3 terms:
0 1 1
Fibonacci Sequence Upto 10 terms:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 8 terms:
0 1 1 2 3 5 8 13
Printing the Fibonacci Sequence Up to n Value
Problem Statement
You're given a number n. You need to print the Fibonacci sequence to the closest value less than or equal to n.
Example 1: Let n = 38.
Fibonacci Sequence up to 38: 0 1 1 2 3 5 8 13 21 34
Thus, the output is 0 1 1 2 3 5 8 13 21 34.
Example 2: Let n = 91.
Fibonacci Sequence up to 91: 0 1 1 2 3 5 8 13 21 34 55 89
Thus, the output is 0 1 1 2 3 5 8 13 21 34 55 89.
C++ Program to Print the Fibonacci Sequence Up to n Value
Below is the C++ program to print the Fibonacci sequence up to the n value:
// C++ program to print the fibonacci sequence upto n value
#include <iostream>
using namespace std;
void printFibonacciSequence(int n)
{
int a = 0, b = 1;
int sum = 0;
cout << "Fibonacci Sequence Upto " << n << ":" << endl;
while(sum <= n)
{
cout << sum << " ";
a = b;
b = sum;
// Next term is the sum of the last two terms
sum = a + b;
}
cout << endl;
}
int main()
{
int n1 = 38;
printFibonacciSequence(n1);
int n2 = 56;
printFibonacciSequence(n2);
int n3 = 12;
printFibonacciSequence(n3);
int n4 = 91;
printFibonacciSequence(n4);
int n5 = 33;
printFibonacciSequence(n5);
return 0;
}
Output:
Fibonacci Sequence Upto 38:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 56:
0 1 1 2 3 5 8 13 21 34 55
Fibonacci Sequence Upto 12:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 91:
0 1 1 2 3 5 8 13 21 34 55 89
Fibonacci Sequence Upto 33:
0 1 1 2 3 5 8 13 21
Python Program to Print the Fibonacci Sequence Up to n Value
Below is the Python program to print the Fibonacci sequence up to the n value:
# Python program to print the fibonacci sequence upto n value
def printFibonacciSequence(n):
a = 0
b = 1
sum = 0
print("Fibonacci Sequence Upto", n, ":")
while (sum<=n):
print(sum, end=" ")
a = b
b = sum
# Next term is the sum of the last two terms
sum = a + b
print()
n1 = 38
printFibonacciSequence(n1)
n2 = 56
printFibonacciSequence(n2)
n3 = 12
printFibonacciSequence(n3)
n4 = 91
printFibonacciSequence(n4)
n5 = 33
printFibonacciSequence(n5)
Output:
Fibonacci Sequence Upto 38:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 56:
0 1 1 2 3 5 8 13 21 34 55
Fibonacci Sequence Upto 12:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 91:
0 1 1 2 3 5 8 13 21 34 55 89
Fibonacci Sequence Upto 33:
0 1 1 2 3 5 8 13 21
JavaScript Program to Print the Fibonacci Sequence Up to n Value
Below is the JavaScript program to print a Fibonacci sequence up to the n value:
// JavaScript program to print the fibonacci sequence upto n value
function printFibonacciSequence(n) {
let a = 0, b = 1;
let sum = 0;
document.write("Fibonacci Sequence Upto " + n + ":" + "
");
while(sum <= n)
{
document.write(sum + " ");
a = b;
b = sum;
// Next term is the sum of the last two terms
sum = a + b;
}
document.write("
");
}
let n1 = 38;
printFibonacciSequence(n1);
let n2 = 56;
printFibonacciSequence(n2);
let n3 = 12;
printFibonacciSequence(n3);
let n4 = 91;
printFibonacciSequence(n4);
let n5 = 33;
printFibonacciSequence(n5);
Output:
Fibonacci Sequence Upto 38:
0 1 1 2 3 5 8 13 21 34
Fibonacci Sequence Upto 56:
0 1 1 2 3 5 8 13 21 34 55
Fibonacci Sequence Upto 12:
0 1 1 2 3 5 8
Fibonacci Sequence Upto 91:
0 1 1 2 3 5 8 13 21 34 55 89
Fibonacci Sequence Upto 33:
0 1 1 2 3 5 8 13 21
Rectify Your Programming Mistakes
Everyone makes mistakes while programming. But these mistakes can lead to so many problems. It's very important to write clean and efficient code while programming. How do you go about that?
You must avoid common programming mistakes like repetitive code, bad variable names, not using comments, language overload, not backing up code, writing complicated code, not planning in advance, not asking questions, etc. Rectifying these mistakes can help you to become a better programmer.