Fibonacci series

Here given code implementation process.

//C Program
//Fibonacci series
//By dynamic programming
#include<stdio.h>

//Display Fibonacci series of given size
void fib(int size)
{
  if(size<=1)
  {
    printf("%d\n",0 );
  }
  else
  {
    //Store fibonacci series
    int result[size];
    
    //Set initial values
    result[0] = 0 ;
    result[1] = 1 ;

    for (int i = 2; i < size; ++i)
    {
      //Use old transaction value
      result[i] = result[i-1] + result[i-2];
    }
    //Show fibonacci series  
    for (int i = 0; i < size; ++i)
    {
      printf("%3d",result[i]);
    }
    printf("\n");
  }
  
}

int main()
{
  //Test Case
  fib(5);
  fib(12);
  return 0;
}

Output

  0  1  1  2  3
  0  1  1  2  3  5  8 13 21 34 55 89
// C++ Program
//Fibonacci series
//By dynamic programming
#include<iostream>

using namespace std;
class DynamicExample {
	public:

    //Display Fibonacci series of given size
    void fibonacci(int size) {
      if (size <= 1) {
        cout << "0\n";
      } else {
        int *result = new int[size];
        //Set initial values
        result[0] = 0;
        result[1] = 1;
        for (int i = 2; i < size; ++i) {
          //Use old transaction value
          result[i] = result[i - 1] + result[i - 2];
        }
        //Show fibonacci series  

        for (int i = 0; i < size; ++i) {
          cout << " " << result[i];
        }
        cout << "\n";
      }
    }
};
int main() {
	DynamicExample obj =  DynamicExample();
	//Test Case
	obj.fibonacci(5);
	obj.fibonacci(12);
	return 0;
}

Output

 0 1 1 2 3
 0 1 1 2 3 5 8 13 21 34 55 89
// Java Program
//Fibonacci series
//By dynamic programming

public class DynamicExample
{
  //Display Fibonacci series of given size
  public void fibonacci(int size) 
  {
    if (size <= 1) 
    {
      System.out.print("0\n");
    } 
    else 
    {
      //Store fibonacci series
      int[] result = new int[size];
      
      //Set initial values
      result[0] = 0;
      result[1] = 1;
     
      for (int i = 2; i < size; ++i) 
      {
        //Use old transaction value
        result[i] = result[i - 1] + result[i - 2];
      }
      //Show fibonacci series  

      for (int i = 0; i < size; ++i) 
      {
        System.out.print("  "+ result[i]);
      }
      System.out.print("\n");
    }
  }

  public static void main(String[] args) 
  {
    DynamicExample obj = new DynamicExample();
    //Test Case
    obj.fibonacci(5);
    obj.fibonacci(12);

  }
}

Output

 0 1 1 2 3
 0 1 1 2 3 5 8 13 21 34 55 89
// C# Program
// Fibonacci series
// By dynamic programming
using System;
public class DynamicExample {
	//Display Fibonacci series of given size
	public void fibonacci(int size) {
		if (size <= 1) {
			Console.Write("0\n");
		} else {
			int[]
			//Store fibonacci series
			result = new int[size];
			//Set initial values
			result[0] = 0;
			result[1] = 1;
			for (int i = 2; i < size; ++i) {
				//Use old transaction value
				result[i] = result[i - 1] + result[i - 2];
			}
			//Show fibonacci series  

			for (int i = 0; i < size; ++i) {
				Console.Write(" " + result[i]);
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args) {
		DynamicExample obj = new DynamicExample();
		obj.fibonacci(5);
		obj.fibonacci(12);
	}
}

Output

 0 1 1 2 3
 0 1 1 2 3 5 8 13 21 34 55 89
<?php
// Php Program
//Fibonacci series
//By dynamic programming
class DynamicExample {
	//Display Fibonacci series of given size

	public 	function fibonacci($size) {
		if ($size <= 1) {
			echo("0\n");
		} else {
			//Store fibonacci series
			$result = array_fill(0, $size, 0);
			//Set initial values
			$result[0] = 0;
			$result[1] = 1;
			for ($i = 2; $i < $size; ++$i) {
				//Use old transaction value
				$result[$i] = $result[$i - 1] + $result[$i - 2];
			}
			//Show fibonacci series  

			for ($i = 0; $i < $size; ++$i) {
				echo(" ". $result[$i]);
			}
			echo("\n");
		}
	}
}

function main() {
	$obj = new DynamicExample();
	//Test Case
	$obj->fibonacci(5);
	$obj->fibonacci(12);

}
main();

Output

 0 1 1 2 3
 0 1 1 2 3 5 8 13 21 34 55 89
// Node Js Program
//Fibonacci series
//By dynamic programming
class DynamicExample {
	//Display Fibonacci series of given size
	fibonacci(size) {
		if (size <= 1) {
			process.stdout.write("0\n");
		} else {
			//Store fibonacci series
			var result = Array(size).fill(0);
			//Set initial values
			result[0] = 0;
			result[1] = 1;
			for (var i = 2; i < size; ++i) {
				//Use old transaction value
				result[i] = result[i - 1] + result[i - 2];
			}

			//Show fibonacci series  

			for (var i = 0; i < size; ++i) {
				process.stdout.write(" " + result[i]);
			}

			process.stdout.write("\n");
		}
	}
}

function main(args) {
	var obj = new DynamicExample();
	//Test Case
	obj.fibonacci(5);
	obj.fibonacci(12);
}

main();

Output

 0 1 1 2 3
 0 1 1 2 3 5 8 13 21 34 55 89
#  Python 3 Program
# Fibonacci series
# By dynamic programming

class DynamicExample :
	# Display Fibonacci series of given size
	def fibonacci(self, size) :
		if (size <= 1) :
			print("0\n", end = "")
		else :
			result = [0] * size
			# Set initial values
			result[0] = 0
			result[1] = 1
			i = 2
			while (i < size) :
				# Use old transaction value
				result[i] = result[i - 1] + result[i - 2]
				i += 1
			
			# Show fibonacci series  
			i = 0
			while (i < size) :
				print(" ", result[i], end = "")
				i += 1
			
			print("\n", end = "")
		
	

def main() :
	obj = DynamicExample()
	obj.fibonacci(5)
	obj.fibonacci(12)


if __name__ == "__main__":
	main()

Output

  0  1  1  2  3
  0  1  1  2  3  5  8  13  21  34  55  89
# Ruby Program
# Fibonacci series
# By dynamic programming
class DynamicExample 
	# Display Fibonacci series of given size
	def fibonacci(size) 
		if (size <= 1) 
			print("0\n")
		else 
			result = Array.new(size) {0}
			# Set initial values
			result[0] = 0
			result[1] = 1
			i = 2
			while (i < size) 
				# Use old transaction value
				result[i] = result[i - 1] + result[i - 2]
				i += 1
			end
			# Show fibonacci series  
			i = 0
			while (i < size) 
				print(" ", result[i])
				i += 1
			end
			print("\n")
		end
	end
end
def main() 
	obj = DynamicExample.new()
	obj.fibonacci(5)
	obj.fibonacci(12)
end
main()

Output

 0 1 1 2 3
 0 1 1 2 3 5 8 13 21 34 55 89
// Scala Program
//Fibonacci series
//By dynamic programming
class DynamicExample {
	//Display Fibonacci series of given size
	def fibonacci(size: Int): Unit = {
		if (size <= 1) {
			print("0\n");
		} else {
			var result: Array[Int] = Array.fill[Int](size)(0);

			//Set initial values
			result(0) = 0;
			result(1) = 1;
			var i: Int = 2;
			while (i < size) {
				//Use old transaction value
				result(i) = result(i - 1) + result(i - 2);
				i += 1;
			}
			//Show fibonacci series  
			i = 0;
			while (i < size) {
				print(" " + result(i));
				i += 1;
			}
			print("\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: DynamicExample = new DynamicExample();
		obj.fibonacci(5);
		obj.fibonacci(12);
	}
}

Output

 0 1 1 2 3
 0 1 1 2 3 5 8 13 21 34 55 89
// Swift Program
//Fibonacci series
//By dynamic programming
class DynamicExample {
	//Display Fibonacci series of given size
	func fibonacci(_ size: Int) {
		if (size <= 1) {
			print("0\n", terminator: "");
		} else {
			var result: [Int] = Array(repeating: 0, count: size);
			//Set initial values
			result[0] = 0;
			result[1] = 1;
			var i: Int = 2;
			while (i < size) {
				//Use old transaction value
				result[i] = result[i - 1] + result[i - 2];
				i += 1;
			}
			//Show fibonacci series  
			i = 0;
			while (i < size) {
				print(" ", result[i], terminator: "");
				i += 1;
			}
			print("\n", terminator: "");
		}
	}
}
func main() {
	let obj: DynamicExample = DynamicExample();
	obj.fibonacci(5);
	obj.fibonacci(12);
}
main();

Output

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


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