Fibonacci series Program in Java/ Python/ PHP/ C/ C++ with using Recursion
What is Fibonacci series?
Fibonacci series is one of the most popular formulas in mathematics. It is a collection of numbers, which starts from one or a zero, and the next terms are generated by sum of the previous two numbers in the series. It is said that it was first invented by an Italian mathematician named Leonardo Fibonacci, who was born approximately A.D. 1170. Keith Devlin said the original name of Leonardo Fibonacci was Leonardo of Pisa, and Leonardo of Pisa was not truly founder of the series, but Devlin was also the father of ‘Finding Fibonacci.’
For example: The series goes 0, 1, 1, 2, 3, 5, 8, 13, and continue, in this series 0 and 1 are fixed, and the next terms come by adding previous two numbers like 0+1=1, 1+1=2, 2+1=3, 3+2=5, and so on. It is described by a mathematical equation that is Tn+2 = Tn+1 + Tn.
Fibonacci series in C:
Logical concept of Fibonacci series in C Programming:
Source code for C language:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=-1,b=1,c,n,i; // assign values a=-1, b=1 for starting series from 0
clrscr();
printf(“enter any number as an input ”)
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
c=a+b;
printf(“%d”,c);
a=b;
b=c;
}
getch();
}
Output:
#include<iostream.h>
#include<conio.h>
void main()
{
int a=-1, b=-1,c,n,i;
clrscr();
cout<<” enter any number as an input “;
cin>>n;
for(i=1;i<=n;i++)
{
c=a+b;
a=b;
b=c;
cout<<’ ’ <<c;
}
getch();
}
Output:
Fibonacci series in Python:
n=int(input(“enter limit”))
a,b,c=-1,1,0
for i in range(1,n+1):
c=a+b
a=b
b=c
print(c)
Output:
<!Doctype html>
<html>
<body>
<h3> Fibonacci Series </h3>
<?php
$a=-1;
$b=1;
$c=0;
for($i=1;$i<=10;$i++)
{
$c=$a+$b;
echo $c.” “;
$a=$b;
$b=$c;
}
?>
</body>
</html>
Output:
Fibonacci series in Java without based on user input:
class fibonacci
{
public static void main(String args[])
{
int a=-1,b=1,c,i;
for(i=1;i<=10;i++)
{
c=a+b;
a=b;
b=c;
System.out.print(c+” “);
}
}
}
Output:
Fibonacci series in Java based on user input:
import java.util.Scanner;
public class Fibs{
public static void main(String[] args) {
int FLength;
Scanner sc = new Scanner(System.in);
System.out.print(“Eenter the limit of the series: “);
FLength = sc.nextInt();
int[] n = new int[FLength];
n[0] = 0;
n[1] = 1;
for (int i = 2; i < FLength; i++)
{
n[i] = n[i — 1] + n[i — 2];
}
System.out.println(“Fibonacci Series: “);
for (int i = 0; i < FLength; i++) {
System.out.print(n[i] + “ “);
}
}
}
Output:
Fibonacci series using recursion function
What is recursion function?
When a function calls itself while its execution, that is known as recursion function. The recursion function finishes if the problem is reduced and go towards a base case. A base case is called where the problem can be solved without any more recursion. It can be executed the infinite loop if the base case is not met the solution.
It allows the function to repeat themselves several times and enables the programmers to write an efficient program using the minimum number of codes. It is very powerful to solve many mathematical problems, like generating the Fibonacci series, calculating the factorial number, etc.
Fibonacci Series Using Recursion in C:
#include<stdio.h>
#include<conio.h>
void main()
{
int fib(int);
int i,n;
clrscr();
printf(“enter the limit of series “);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
printf(“ %d”,fib(i));
getch();
}
int fib(int n)
{
if(n==1||n==2)
return(1);
return(fib(n-1)+fib(n-2));
}
Output:
Fibonacci Series Using Recursion in Java:
class FiboUsingRec
{
static int a=0,b=1,c;
public static void main(String args[])
{
System.out.print(a+” “+b);
FiboUsingRec ob=new FiboUsingRec();
ob.printFib(12);
}
void printFib(int i)
{
if(i>=1)
{
c=a+b;
System.out.print(“ “ +c);
a=b;
b=c;
printFib(i-1);
}
}
}
Output:
Fibonacci Series Using Recursion in Python:
def fib(n1,n2,n):
if n==0:
return
res=n1+n2
print(res)
fib(n2,res,n-1)
a=0
b=1
n=int(input(“Enter the limit of the series”))
print(a)
print(b)
fib(a,b,n-2)
Output:
Fibonacci Series Using Recursion in PHP:
<html>
<head>
<title>Fibonacci series using recursion in PHP</title>
</head>
<body>
<form method=”post”>
Enter the limit:
<input type=”text” name=”number”>
<input type=”submit” value=”Submit”>
</form>
</body>
</html>
<?php
if($_POST)
{
$n = $_POST[‘number’];
echo “<h2>Fibonacci series using recursion function:</h2>”;
echo “\n”;
/* Recursive function */
function series($n){
if($n == 0){
return 0;
}else if( $n == 1){
return 1;
} else {
return (series($n-1) + series($n-2));
}
}
/* Calling Function */
for ($i = 0; $i < $n; $i++){
echo series($i);
echo “\n”;
}
}
Output: