Fibonacci sequence
The Fibonacci sequence is a sequence of numbers in which each number is the sum of the two preceding numbers. The sequence starts with 0 and 1, and continues as follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, ...
To obtain each term of the sequence, you add the two previous terms. For example, to obtain the third term, you add the first and second terms:
0 + 1 = 1
To obtain the fourth term, you add the second and third terms:
1 + 1 = 2
And so on.
The Fibonacci sequence has many interesting properties and is found in many natural phenomena, including the growth patterns of plants and the arrangement of leaves on stems. It also arises in many areas of mathematics, including number theory, algebra, and combinatorics.
- Print fibonacci sequence in java
- Print fibonacci sequence in c++
- Print fibonacci sequence in c
- Print fibonacci sequence in golang
- Print fibonacci sequence in c#
- Print fibonacci sequence in vb.net
- Print fibonacci sequence in php
- Print fibonacci sequence in node js
- Print fibonacci sequence in typescript
- Print fibonacci sequence in python
- Print fibonacci sequence in ruby
- Print fibonacci sequence in scala
- Print fibonacci sequence in swift
- Print fibonacci sequence in kotlin
Print fibonacci sequence in java
/*
Java program for
Display fibonacci series of given size
*/
public class Numbers
{
public void fibonacci(int n)
{
// Set the initial value of variable
// This is two initial value.
int first = 0;
int second = 1;
// Print series
while (n > 0)
{
System.out.print(" " + first);
// This is next result
second += first;
first = second - first;
n--;
}
}
public static void main(String[] args)
{
Numbers task = new Numbers();
// Test
task.fibonacci(15);
}
}
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Print fibonacci sequence in c++
// Include header file
#include <iostream>
// Stdc++11 program for
// Display fibonacci series of given size
class Numbers
{
public:
void fibonacci(int n)
{
// Set the initial value of variable
// This is two initial value.
int first = 0;
int second = 1;
// Print series
while (n > 0)
{
std::cout << " "<< first;
// This is next result
second += first;
first = second - first;
n--;
}
}
};
int main(int argc, char **argv){
Numbers task ;
// Test
task.fibonacci(15);
return 0;
};
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Print fibonacci sequence in c
// Include header file
#include <stdio.h>
// C program for
// Display fibonacci series of given size
void fibonacci (int n)
{
// Set the initial value of variable
// This is two initial value.
int first = 0;
int second = 1;
// Print series
while(n > 0)
{
printf(" %d" , first);
// This is next result
second += first;
first = second - first;
n--;
}
}
int main()
{
// Test
fibonacci(15);
return 0;
}
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Print fibonacci sequence in golang
package main
import "fmt"
// Golang program for
// Display fibonacci series of given size
func fibonacci(n int) {
// Set the initial value of variable
// This is two initial value.
var first int = 0;
var second int = 1;
// Print series
for(n > 0) {
fmt.Printf(" %d",first);
// This is next result
second += first;
first = second - first;
n--;
}
}
func main() {
// Test
fibonacci(15);
}
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Print fibonacci sequence in c#
// Include namespace system
using System;
// C# program for
// Display fibonacci series of given size
public class Numbers
{
public void fibonacci(int n)
{
// Set the initial value of variable
// This is two initial value.
var first = 0;
var second = 1;
// Print series
while (n > 0)
{
Console.Write(" " + first);
// This is next result
second += first;
first = second - first;
n--;
}
}
public static void Main(String[] args)
{
var task = new Numbers();
// Test
task.fibonacci(15);
}
}
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Print fibonacci sequence in vb.net
' Include namespace system
Imports System
' Vb.net program for
' Display fibonacci series of given size
public Class Numbers
Public Sub fibonacci(ByVal n As Integer)
' Set the initial value of variable
' This is two initial value.
Dim first As Integer = 0
Dim second As Integer = 1
' Print series
while (n > 0)
Console.Write(" " + first.ToString())
' This is next result
second += first
first = second - first
n -= 1
End While
End Sub
Public Shared Sub Main(ByVal args As String())
Dim task As Numbers = New Numbers()
' Test
task.fibonacci(15)
End Sub
End Class
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Print fibonacci sequence in php
<?php
// Php program for
// Display fibonacci series of given size
class Numbers
{
function fibonacci($n)
{
// Set the initial value of variable
// This is two initial value.
$first = 0;
$second = 1;
// Print series
while ($n > 0)
{
echo " ",$first;
// This is next result
$second += $first;
$first = $second - $first;
$n--;
}
}
}
$task = new Numbers();
// Test
$task->fibonacci(15);
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Print fibonacci sequence in node js
// Node Js program for
// Display fibonacci series of given size
class Numbers
{
fibonacci(n)
{
// Set the initial value of variable
// This is two initial value.
var first = 0;
var second = 1;
// Print series
while (n > 0)
{
console.log(" " + first);
// This is next result
second += first;
first = second - first;
n--;
}
}
}
// Start program execution
var task = new Numbers();
// Test
task.fibonacci(15);
Output
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
Print fibonacci sequence in typescript
// Typescript program for
// Display fibonacci series of given size
class Numbers
{
public fibonacci(n:number)
{
// Set the initial value of variable
// This is two initial value.
var first = 0;
var second = 1;
// Print series
while (n > 0)
{
console.log(" " + first);
// This is next result
second += first;
first = second - first;
n--;
}
}
}
var task = new Numbers();
// Test
task.fibonacci(15);
/*
file : code.ts
tsc --target es6 code.ts
node code.js
*/
Output
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
Print fibonacci sequence in python
# Python 3 program for
# Display fibonacci series of given size
class Numbers :
def fibonacci(self, n) :
# Set the initial value of variable
# This is two initial value.
first = 0
second = 1
# Print series
while (n > 0) :
print(first, end =" ")
# This is next result
second += first
first = second - first
n -= 1
if __name__=="__main__":
task = Numbers()
# Test
task.fibonacci(15)
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Print fibonacci sequence in ruby
# Ruby program for
# Display fibonacci series of given size
class Numbers
def fibonacci( n)
# Set the initial value of variable
# This is two initial value.
first = 0
second = 1
# Print series
while (n > 0)
print(" ",first)
# This is next result
second += first
first = second - first
n -= 1
end
end
end
task = Numbers.new()
# Test
task.fibonacci(15)
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Print fibonacci sequence in scala
// Scala program for
// Display fibonacci series of given size
class Numbers ()
{
def fibonacci(num : Int) : Unit=
{
var n = num;
// Set the initial value of variable
// This is two initial value.
var first = 0
var second = 1
// Print series
while (n > 0)
{
print(" " + first)
// This is next result
second += first
first = second - first
n -= 1
}
}
}
object Main
{
def main(args : Array[String]) : Unit=
{
var task = new Numbers()
// Test
task.fibonacci(15)
}
}
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Print fibonacci sequence in swift
import Foundation
// Swift program for
// Display fibonacci series of given size
class Numbers
{
func fibonacci(_ num: Int)
{
var n = num;
// Set the initial value of variable
// This is two initial value.
var first: Int = 0;
var second: Int = 1;
// Print series
while (n > 0)
{
print(first, terminator: " ");
// This is next result
second += first;
first = second - first;
n -= 1;
}
}
}
let task: Numbers = Numbers();
// Test
task.fibonacci(15);
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Print fibonacci sequence in kotlin
// Kotlin program for
// Display fibonacci series of given size
class Numbers {
fun fibonacci(num : Int) : Unit
{
var n = num;
// Set the initial value of variable
// This is two initial value.
var first : Int = 0;
var second : Int = 1;
// Print series
while (n > 0)
{
print(" " + first);
// This is next result
second += first;
first = second - first;
n -= 1;
}
}
}
fun main(args : Array<String>) : Unit
{
val task : Numbers = Numbers();
// Test
task.fibonacci(15);
}
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment