The below program prints a Fibonacci Series without recursion and with recursion. The C printf statement is used to output the result on the screen. A series is called as a Fibonacci series if the each next term of the series is a sum of previous two numbers.
C program to print fibonacci series
a, b, c, d, e
c = a+b
d = b+c
e = c+d
For example,
0 2 2 4 6 10 16 26
Code:
#include <stdio.h> void main() { int n = 0, a = 0, b = 2; int c; int mod; printf ("Fibonacci series with the first 2 numbers as 0 and 2 is: "); printf ("%d, %d", a, b); for (n=0; n<10; n++) { c = b + a; printf ( ", "); printf ( "%d",c); a = b; b = c; } } |
Output
Fibonacci series with the first 2 numbers as 0 and 2 is: 0, 2, 2, 4, 6, 10, 16, 26, 42, 68, 110, 178 |