What is Fibonacci Series/Sequence
The Fibonacci series is a sequence of numbers (also known as Fibonacci numbers) in which each number is the sum of the two numbers before it, with the first two terms being '0' and '1'. A Fibonacci series can thus be written as 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... It can be thus be observed that every term can be calculated by adding the two terms before it.
Therefore, to represent any (n+1)th
term in this series, we can give the expression as, Fn = Fn-1 + Fn-2
Program In C Language
#include<stdio.h> int main() { int n,i,a=0,b=1,c; printf("Enter Number of Terms:\n"); scanf("%d",&n); printf("The Fibonacci Series is:\n"); for(i=0;i<n;i++) { printf("%d\n",a); c=a+b; a=b; b=c; } return 0; }
Output:
How many terms? 7 Fibonacci sequence: 0 1 1 2 3 5 8
Program In Python Language
def fib(n): a,b=0,1 if n==1: print(a) elif n==2: print(a,b) else: print(a,b,end=" ") i=2 while i!=n: c=a+b print(c,end=" ") a=b b=c i+=1 n=int(input("Enter Number ofTerms:")) print("Fibonacci sequence:") fib(n)
Output:
Enter Number of Terms:7 Fibonacci sequence: 0 1 1 2 3 5 8
Program In Java Language
import java.util.*; class fibo { public static void main(String args[]) { int i,c=0,n; Scanner sc = new Scanner(System.in); System.out.println("Enter Number of Terms:"); n=sc.nextInt(); int a=0; int b=1; System.out.println("Fibonacci Sequence:); for(i=0;i<n;i++) { System.out.print(c+" "); a=b; b=c; c=a+b; } } }
Output:
Enter Number of Terms:7 Fibonacci sequence: 0 1 1 2 3 5 8