![Fibonacci numbers algorithm code in diffrent coding languages](/media/blog_images/fibonacci-number-algorithms.png)
The Fibonacci numbers are a very useful algorithm, so before we continue, here is a short introduction to Fibonacci numbers.
The two first Fibonacci numbers are 0 and 1, and the next Fibonacci number is always the sum of the two previous numbers, so we get 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
To generate a Fibonacci number, all we need to do is add the two previous Fibonacci numbers. We know the principle of how to find the next number, so we can write an algorithm to create as many Fibonacci numbers as possible.
How is it works:
- Add the two previous numbers together to create a new Fibonacci number.
- Update the value of the two previous numbers.
Here is the Fibonacci series code in different programming languages:
#python
# Initialize the first two Fibonacci numbers
n=int(input())
prev2, prev1 = 0, 1
# Print the first two Fibonacci numbers
print(prev2)
print(prev1)
# Loop to generate and print the next 18 Fibonacci numbers
for _ in range(n):
# Calculate the next Fibonacci number by adding the previous two
next_fibo = prev2 + prev1
# Print the next Fibonacci number
print(next_fibo)
# Update prev2 and prev1 for the next iteration
prev2, prev1 = prev1, next_fibo
//C++
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n; // Input number of Fibonacci numbers to generate
int prev2 = 0;
int prev1 = 1;
cout << prev2 << endl; // Print the first Fibonacci number (0)
cout << prev1 << endl; // Print the second Fibonacci number (1)
for (int i = 2; i < n; i++) {
int nextFibo = prev2 + prev1; // Calculate the next Fibonacci number
cout << nextFibo << endl; // Print the next Fibonacci number
// Update prev2 and prev1 for the next iteration
prev2 = prev1;
prev1 = nextFibo;
}
return 0;
}
//Java
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of Fibonacci numbers to generate: ");
int n = scanner.nextInt(); // Input number of Fibonacci numbers to generate
scanner.close();
int prev2 = 0;
int prev1 = 1;
System.out.println(prev2); // Print the first Fibonacci number (0)
System.out.println(prev1); // Print the second Fibonacci number (1)
for (int i = 2; i < n; i++) {
int nextFibo = prev2 + prev1; // Calculate the next Fibonacci number
System.out.println(nextFibo); // Print the next Fibonacci number
// Update prev2 and prev1 for the next iteration
prev2 = prev1;
prev1 = nextFibo;
}
}
}
//JavaScript
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the number of Fibonacci numbers to generate: ', (input) => {
const n = parseInt(input);
let prev2 = 0;
let prev1 = 1;
console.log(prev2); // Print the first Fibonacci number (0)
console.log(prev1); // Print the second Fibonacci number (1)
for (let i = 2; i < n; i++) {
let nextFibo = prev2 + prev1; // Calculate the next Fibonacci number
console.log(nextFibo); // Print the next Fibonacci number
// Update prev2 and prev1 for the next iteration
prev2 = prev1;
prev1 = nextFibo;
}
rl.close();
});
//C#
using System;
class Program
{
static void Main()
{
Console.Write("Enter the number of Fibonacci numbers to generate: ");
int n = int.Parse(Console.ReadLine());
int prev2 = 0;
int prev1 = 1;
Console.WriteLine(prev2); // Print the first Fibonacci number (0)
Console.WriteLine(prev1); // Print the second Fibonacci number (1)
for (int i = 2; i < n; i++)
{
int nextFibo = prev2 + prev1; // Calculate the next Fibonacci number
Console.WriteLine(nextFibo); // Print the next Fibonacci number
// Update prev2 and prev1 for the next iteration
prev2 = prev1;
prev1 = nextFibo;
}
}
}
Do you dream of turning your thoughts and words into income ? join us now and become blogger now.
0 Comments
Leave a Comment