fibonacci sequence

Here given code implementation process.

//C Program
//Fibonacci Series
#include <stdio.h>


void fibonacci(int n)
{
  //Set the initial value of variable
  //This is two initial value
  int first = 0;
  int second = 1;

  while(n > 0)
  {
    printf("  %d",first );
    //This is next result
    second += first;

    first   = second - first;

    n--;
  }
}

int main()
{
 
  int n = 15;
  
  fibonacci(n);

  return 0;
}

Output

  0  1  1  2  3  5  8  13  21  34  55  89  144  233  377
#include<iostream>

using namespace std;

/*
  C++ Program
  Fibonacci Series
*/
class MyNumber {
	public:
		void fibonacci(int n) {
			//Set the initial value of variable
			//This is two initial value
			int first = 0;
			int second = 1;
			while (n > 0) {
				cout << " " << first;
				//This is next result
				second += first;
				first = second - first;
				n--;
			}
		}
};
int main() {
	MyNumber obj;
	int n = 15;
	obj.fibonacci(n);
	return 0;
}

Output

 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
/*
  Java Program
  Fibonacci Series
*/
public class MyNumber {

  public void fibonacci(int n)
  {
    //Set the initial value of variable
    //This is two initial value
    int first = 0;
    int second = 1;

    while(n > 0)
    {
      System.out.print("  "+first );
      //This is next result
      second += first;

      first   = second - first;

      n--;
    }
  }
  public static void main(String[] args) {
  

    MyNumber obj = new MyNumber();
   
    int n = 15;
    
    obj.fibonacci(n);
  }
}

Output

 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
/*
  C# Program
  Fibonacci Series
*/
using System;
public class MyNumber {
	public void fibonacci(int n) {
		//This is two initial value
		//Set the initial value of variable
		//This is two initial value
		int first = 0;
		int second = 1;
		while (n > 0) {
			Console.Write(" " + first);
			//This is next result
			second += first;
			first = second - first;
			n--;
		}
	}
	public static void Main(String[] args) {
		MyNumber obj = new MyNumber();
		int n = 15;
		obj.fibonacci(n);
	}
}

Output

 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
#   Python 3 Program
#   Fibonacci Series
class MyNumber :
	def fibonacci(self, n) :
		# Set the initial value of variable
		# This is two initial value
		first = 0
		second = 1
		while (n > 0) :
			print(" ", first, end = "")
			# This is next result
			second += first
			first = second - first
			n -= 1
		
	

def main() :
	obj = MyNumber()
	n = 15
	obj.fibonacci(n)


if __name__ == "__main__":
	main()

Output

  0  1  1  2  3  5  8  13  21  34  55  89  144  233  377
#   Ruby Program
#   Fibonacci Series
class MyNumber 
	def fibonacci(n) 
		# Set the initial value of variable
		# This is two initial value
		first = 0
		second = 1
		while (n > 0) 
			print(" ", first)
			# This is next result
			second += first
			first = second - first
			n -= 1
		end
	end
end
def main() 
	obj = MyNumber.new()
	n = 15
	obj.fibonacci(n)
end
main()

Output

 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
/*
  Scala Program
  Fibonacci Series
*/
class MyNumber {
	def fibonacci(number: Int): Unit = {
		//Set the initial value of variable
		//This is two initial value
		var first: Int = 0;
		var second: Int = 1;
      	var n: Int = number;
		while (n > 0) {
			print(" " + first);

			//This is next result
			second += first;
			first = second - first;
			n -= 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyNumber = new MyNumber();
		val n: Int = 15;
		obj.fibonacci(n);
	}
}

Output

 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
/*
  Swift Program
  Fibonacci Series
*/
class MyNumber {
	func fibonacci(_ number: Int) {
		//Set the initial value of variable
		//This is two initial value
		var first: Int = 0;
		var second: Int = 1;
      	var n: Int = number;
		while (n > 0) {
			print(" ", first, terminator: "");
			//This is next result
			second += first;
			first = second - first;
			n -= 1;
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	let n: Int = 15;
	obj.fibonacci(n);
}
main();

Output

  0  1  1  2  3  5  8  13  21  34  55  89  144  233  377
<?php
/*
  Php Program
  Fibonacci Series
*/
class MyNumber {
	public 	function fibonacci($n) {
		//Set the initial value of variable
		//This is two initial value
		$first = 0;
		$second = 1;
		while ($n > 0) {
			echo(" ". $first);
			//This is next result
			$second += $first;
			$first = $second - $first;
			$n--;
		}
	}
};

function main() {
	$obj = new MyNumber();
	$n = 15;
	$obj->fibonacci($n);

}
main();

Output

 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
/*
  Node Js Program
  Fibonacci Series
*/
class MyNumber {
	fibonacci(n) {
		//Set the initial value of variable
		//This is two initial value
		var first = 0;
		var second = 1;
		while (n > 0) {
			process.stdout.write(" " + first);
			//This is next result
			second += first;
			first = second - first;
			n--;
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	var n = 15;
	obj.fibonacci(n);
}

main();

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







© 2021, kalkicode.com, All rights reserved