What is a Fibonacci sequence and how do I print one in Python, C ++ and JavaScript?

0

Programming is closely related to puzzles and math. Solving programming puzzles is one way to keep yourself mentally active and in shape. It helps to develop problem solving skills.

The Fibonacci sequence problem is one of those logic programming problems that is fun to solve and also posed in technical interviews. We think this is a great project to hone your arithmetic skills in the language of your choice.

It looks nice? Let’s get started. In this article, you will learn how to print a Fibonacci sequence up to n terms and n value.

What is a Fibonacci sequence?

A Fibonacci sequence is a sequence 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 Fm.

F0 = 0 and F1 = 1.
and
Fn = Fn-1 + Fn-2

Fibonacci sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Printing the first n Fibonacci numbers

Statement of the problem

We give you a number m. You need to print the Fibonacci sequence up to the first m terms.

Example 1: Let n = 5.

5 first Fibonacci numbers: 0 1 1 2 3

So the output is 0 1 1 2 3.

Example 2: Let n = 7.

7 first Fibonacci numbers: 0 1 1 2 3 5 8

So 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
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{
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;
}

Production:

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)

Production:

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

Related: How to Add and Subtract Two Matrices in C ++, Python, and JavaScript

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; idocument.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);

Production:

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 the value n

Statement of the problem

We give you a number m. You must print the Fibonacci sequence to the nearest value less than or equal to m.

Example 1: Let n = 38.

Fibonacci sequence up to 38: 0 1 1 2 3 5 8 13 21 34

So 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

So the output is 0 1 1 2 3 5 8 13 21 34 55 89.

Related: How to Find the Sum of Natural Numbers Using Recursion

C ++ program to print the Fibonacci sequence up to n value

Below is the C ++ program to print the Fibonacci sequence up to the value n:

// C++ program to print the fibonacci sequence upto n value
#include
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;
}

Production:

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

Related: How to Find LCM and GCD of Two Numbers in Multiple Languages

Python program to print the Fibonacci sequence up to n value

Below is the Python program to print the Fibonacci sequence up to the value n:

# 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)

Production:

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

Related: How to Create and Decode a QR Code Using Python

JavaScript program to print the Fibonacci sequence up to n value

Below is the JavaScript program to print a Fibonacci sequence up to the value n:

// 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);

Production:

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

Correct your programming errors

Everyone makes mistakes while programming. But these mistakes can lead to so many problems. It is very important to write clean and efficient code when programming. How do you do it?

You should avoid common programming errors like repetitive code, bad variable names, not using comments, language overload, not saving code, writing complicated code, failing to plan ahead, not to ask questions, etc. best programmer.


programming-errors
10 most common programming and coding mistakes

Coding errors can lead to so many problems. These tips will help you avoid programming errors and keep your code meaningful.

Read more


About the Author

.


Source link

Leave A Reply

Your email address will not be published.