Online Tutorials & Training Materials | STechies.com
Register Login

Fibonacci Series Program in Java, Python, C, C++

|| 0

Fibonacci Series Program in Java, Python, C, C++
Stechies

What is the Fibonacci Series?

A Fibonacci series is a sequence of numbers where each number is the sum of the previous two numbers. The series starts from two numbers 0 and 1. They are in the fowling sequence:

This series generates next number in series by adding the previous two numbers. It starts the sequence of series using two numbers − F0 & F1. With the starting values of F0 & F1 to start the series 0, 1 or 1, 1 respectively.

either 
0,1,1,2,3,5,8,13,21,34,55,.,.,.,.,.,.,. and so on

or if it starts with 1 then
1,1,2,3,5,8,11,19,30,49,79........ and so on.

Fibonacci Series Examples

In mathematical terminology, each number is in the form of:

Fn = Fn-1 + Fn-2

In this tutorial, we will Study

Fibonacci Series Algorithm

Fibonacci sequence algorithm starts with 1 we can write it as

sq (1) = sq (0) = 1.

sq (n) = sq ( n-1 ) + sq ( n-2 )  for n >= 2

Fibonacci numbers starts with (starting sq (0))  1, 1, 2, 3, 5, 8 ...

simply we can say the n-th Fibonacci number is the sum of the prior 2. As said in the explanation.

If you want to start with 0 then you have assigned 0 to sq (1)=sq(0)=0.

To understand in the elaborated way of Algorithm

1) Start

2) Variables Declaration L, A, B, display

3) Value assign to variables, A=0, B=1, and Display =0

4) Assign the series sequence to be run

5) Print starting two number terms of series using variable previously caring values 0,1 (in A, B respectively).

6) Start the loop using the loop variable

  • -> Display =A+B
  • -> A=B
  • -> B=Display
  • -> increase loop value of L by 1
  • -> print the value of Display

7) End

Fibonacci Series Methods

There are 2 methods to Display Fibonacci Sequence:

  1. Fibonacci Series using Recursion
  2. Fibonacci Series without Recursion

Fibonacci Series using Recursion in C

#include<stdio.h>
void main()
{
int fibonacci(int ) ;
    int i,s,n;
    printf("Number of elements in the list : ");
    scanf("%d",&n);
    printf("Fibonacci series is :n");
    for(i=1;i<=n;i++)
    {
 s= fibonacci(i);// Calling with Argument
 printf("%5d",s);
   }
getch();
}

// Function definition
int fibonacci(int x)
{
 int f;
 if (x==1)
  return(0);
 else if (x==2)
 return(1);
else
f= fibonacci(x-1)+fibonacci(x-2);
 return(f);
}

Output

Number of elements in the list: 5

Fibonacci series is: 0 1 1 2 3 5

Fibonacci series is:

0 1 1 2 3

Fibonacci Series without Recursion in C

#include<stdio.h>    
int main()    
{    
 int n1=0,n2=1,n3,i,number;    
 printf("Number of elements in the list:");    
 scanf("%d",&number);    
 printf("n%d %d",n1,n2);   
 for(i=2;i<number;++i)  
 {    
  n3=n1+n2;    
  printf(" %d",n3);    
  n1=n2;    
  n2=n3;    
 }  
  return 0;  
 }    

Output

Number of elements in the list: 5

Fibonacci series is: 0 1 1 2 3

Fibonacci Series using Function

#include<stdio.h>
void main()
{
int fibonacci(int,int),n,i;
printf("Number of elements in the list:");
scanf("%d",&n);
printf("  0  1");
fibonacci(i,n);
getch();
}
int fibonacci(int m,int n)
{
int a=0,b=1;
int c;
for(m=1;m<=n-2;m++)
{
c=a+b;
a=b;
b=c;
printf("t%d",c);
}
}

Output

Number of elements in the list: 4

Fibonacci series is:

0 1 1 2

Fibonacci Series Java Program Example

public class Main {
 public static void main(String args[]) {
 int val1, val2, val3, loi, nxt;
 nxt = 10;
 val1 = val2 = 1;
 System.out.print("Fibonacci Series ");
 System.out.print(val1+" "+val2);
 for(loi = 1; loi <= nxt-2; loi++) {
 val3 = val1 + val2;
 System.out.print(" ");
 System.out.print(val3);
 val1 = val2;
 val2 = val3;
 }
 }
}

Output will be

$javac FibSeries.java
$java -Xmx128M -Xms16M FibSeries
Fibonacci Series  1 1 2 3 5 8 13 21 34 55

Fibonacci Series in C

#include <stdio.h>

int main()

{
 int i, nt, num1 = 0, num2 = 1, nTerm;

 printf("Enter the number of terms: ");

 scanf("%d", &nt);

 printf("Fibonacci Series: ");
 for (i = 1; i <= nt; ++i)

 {
 printf("%d, ", num1);

 nTerm = num1 + num2;

 num1 = num2;

 num2 = nTerm;

 }

 return 0;

}

Output will be

Enter the number of terms: 12                                                                                                 

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,  

Fibonacci Series Python Example

# To print Fibonacci series, after taking no of terms input from user
loopterms = int(input("Please enter the Loop Terms : "))
num1 = 0
num2 = 1
total = 2
if loopterms <= 0:
 print("Plese enter a positive integer")
elif loopterms == 1:
 print("Fibonacci sequence:")
 print(num1)
else:
 print("Fibonacci sequence:")
 print(num1,",",num2,end=', ')
 while total < loopterms:
  nth = num1 + num2
  print(nth,end=' , ')
  num1 = num2
  num2 = nth
  total += 1

Output will be 

Please enter the Loop Terms : 10                                                                                                            

Fibonacci sequence:                                                                                                            
0 , 1, 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 

Fibonacci Series C++ Program Example

#include <iostream>

using namespace std;

int main()

{
 int num1 = 0,i=2, num2 = 1, nTerm = 0, nput;

 cout << "Enter a positive number: ";

 cin >> nput;

 // displays the first two terms which is always 0 and 1

 cout << "Fibonacci Series: " << num1 << ", " << num2 << ", ";

 nTerm = num1 + num2;

 while(i < nput)

 {
 ++i;

 cout << nTerm << ", ";

 num1 = num2;

 num2 = nTerm;

 nTerm = num1 + num2;
 }

 return 0;

}

Output will be 

Enter the number of terms: 12                                                                                                 

Fibonacci Series:  01 1 2 3 5 8 13 21 34 55 89