C Program to generate Fibonacci Series for first n natural numbers
Program
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i, range;
int term_one, term_two, fib;
printf("Enter the range of fibonacci series\n");
scanf("%d", &range);
if(range <= 0)
{
printf("Cannot generate series.\nExiting Program\n");
exit(0);
}
term_one = 0;
term_two = 1;
printf("Fibonacci Series:\n");
if(range == 1)
printf("%d", term_one);
else if(range >= 2)
{
printf("%d\t%d\t", term_one, term_two);
for(i = 0; i < range - 2; i++)
{
fib = term_one + term_two;
printf("%d\t", fib);
term_one = term_two;
term_two = fib;
}
}
printf("\n");
}
Output
Enter the range of fibonacci series
6
Fibonacci Series:
0 1 1 2 3 5