C Program to Find First N Fibonacci Numbers

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.

  1. /*
  2.  * C program to generate and print first N FIBONACCI numbers
  3.  * in the series.
  4.  */
  5. #include <stdio.h>
  6.  
  7. void main()
  8. {
  9.     int fib1 = 0, fib2 = 1, fib3, num, count = 0;
  10.  
  11.     printf("Enter the value of num \n");
  12.     scanf("%d", &num);
  13.     printf("First %d FIBONACCI numbers are ...\n", num);
  14.     printf("%d\n", fib1);
  15.     printf("%d\n", fib2);
  16.     count = 2; /* fib1 and fib2 are already used */
  17.     while (count < num)
  18.     {
  19.         fib3 = fib1 + fib2;
  20.         count++;
  21.         printf("%d\n", fib3);
  22.         fib1 = fib2;
  23.         fib2 = fib3;
  24.    }
  25. }

$ 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.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

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.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.