generate a Fibonacci series of n numbers using c program




Fibonacci series are numbers generated by adding a number with number previous of that number.

The first two numbers in the Fibonacci series are either 1 and 1, or 0 and 1, depending on the chosen starting point of the series, and each  number is the sum of the previous two numbers.
mathematical formula to find the fibonacci series is given below
F_n = F_{n-1} + F_{n-2},\!\,

First two numbers are 0 and 1 that we have to give manually, then o+1 is 1, now add 1+1 is 2 and so on. so the Fibonacci series is 0,1,1,2,3,5,... C program to generate N (value that users enters) Fibonacci numbers is by adding the numbers with previous number.


Here we need to use a count variable initialized to 2 in beginning to check N is reached.

===================================================================
#include <stdio.h>
 
void main()
 
{
 
  int   fib1=0, fib2=1, fib3, N, count=0;
 
  printf("Enter the value of N\n");    /* enter the value from the user  */
 
  scanf("%d", &N);
 
  printf("First %d FIBONACCI numbers are ...\n", N);
 
  printf("%d\n",fib1);
 
  printf("%d\n",fib2);
 
  count = 2;                          /* fib1 and fib2 are already used */
 
  while( count < N)
 
  {
 
    fib3 = fib1 + fib2;               /* while loop for generating first N Fibonacci numbers  */
 
    count ++;
 
    printf("%d\n",fib3);              /* display the Fibonacci series  */
 
    fib1 = fib2;
 
    fib2 = fib3;                      /* changing the variables */ 
 
  }
 
} 	/* End of main() */
==================================================================
OUTPUT of C program to generate N FIBONACCI numbers
==================================================================
Enter the value of N
10
First 5 FIBONACCI numbers are ...
0
1
1
2
3
5
8
13
21
34