C Programming | Fibonacci Series
The phrase, “the Fibonacci sequence” usually refers to a set of numbers that starts as {0, 1, 1, 2, 3, 5, 8, 13, 21…}. At least, “The Life and Numbers of Fibonacci” starts with “zero and one”. Many others would skip the zero and simply start with “one and one”, but that does not matter.
This sequence is created by following two rules:
- The first two numbers are 0 and 1.
- The next number is the sum of the two most recent numbers.
Actually, one must perform rule #2 over and over again, until one runs out of time, patience, paper or ink in the pen.
So, constructing the Fibonacci sequence starts from 1=1+0, then 2=1+1, then 3=2+1, then 5=3+2, 8=5+3, and so on.
C Program for Fibonacci Series
#include <stdio.h>#include <conio.h>int fibonacci(int term);int main(){int terms, counter;printf("Enter number of terms in Fibonacci series: ");scanf("%d", &terms);/** Nth term = (N-1)th therm + (N-2)th term;*/printf("Fibonacci series till %d terms\n", terms);for (counter = 0; counter < terms; counter++){printf("%d ", fibonacci(counter));}getch();return 0;}/** Function to calculate Nth Fibonacci number* fibonacci(N) = fibonacci(N - 1) + fibonacci(N - 2);*/int fibonacci(int term){/* Exit condition of recursion*/if (term < 2)return term;return fibonacci(term - 1) + fibonacci(term - 2);}
C Code to Find Fibonacci Series
#include <stdio.h>int main(){int i, first = 0, second = 1, third, n;printf("Enter number of terms \n");scanf("%d", &n);printf("The First terms of fibonisse series are:");for (i = 0; i < n; i++){if (i <= 1){third = i;}else{third = first + second; // This changes the values.first = second;second = third;}printf("%d\n", third);}return 0;}
Post A Comment:
0 comments so far,add yours