This C Program calculate the Fibonacci numbers in the series. The first two numbers in the Fibonacci sequence are 0 and 1 and each subsequent number is the sum of the previous two. The formula for this program is: Fn = Fn-1 + Fn-2
Here is source code of the C program to calculate the Fibonacci Numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C program to generate and print first N FIBONACCI numbers
* in the series.
int fib1 = 0, fib2 = 1, fib3, num, count = 0;
printf("Enter the value of num \n");
printf("First %d FIBONACCI numbers are ...\n", num);
count = 2; /* fib1 and fib2 are already used */
while (count < num)
fib3 = fib1 + fib2;
fib1 = fib2;
fib2 = fib3;
$ cc pgm10.c $ a.out Enter the value of num 15 First 15 FIBONACCI numbers are ... 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Sanfoundry Global Education & Learning Series – 1000 C Programs.
If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.