C program to Generate First n Fibonacci Numbers
Description :
This program accepts one number from user, let’s say it as n and prints first n Fibonacci Numbers on Console.
Program :

/*

C Program to Generate first n Fibonacci Numbers

this program accepts one number from user let’s say it as n

and prints first n Fibonacci Numbers on Console.

Author : Venkatesh Macha

*/

#include<stdio.h>

int main()

{

int i,j,k,num,cnt;

printf(“How many fibonacci numbers do you want to print : “);

scanf(“%d”,&num);

i=0;

j=1;

/* because fibonacci series starts with 0,1

* so i initialized i and j as 0 and 1

*/

printf(“Fibonacci Series is : “);

if(num == 1)

{

printf(” %dn“,i);

return;

}

printf(” %d %d “,i,j);

cnt=2;

// cnt=2 because we already printed i,j in above statement.

k=i+j;

while(num > cnt)

{

printf(“%d “,k);

i=j;

j=k;

k=i+j;

cnt++;

}

printf(“n“);

}
Output :
Note : This program generates up to any number of Fibonacci Series but You need to use sufficient Datatype for the our i,j,k variables. In above program i used Integer datatype for i,j,k variables, So it will generates up to 2 giga number after that it will give Negative numbers. So Use Long int or Long long int depending on your Requirement. You can refer this post to know the basic Datatypes in and Size of those Datatypes.