Home Tools

Write a Fibonacci Series in C

The Fibonacci sequence is a series of numbers where a number is the addition of the last two numbers, starting with 0, and 1.
fibonacci.c
1#include <stdio.h>
2int main() {
3	int i=0,j=0,t=0,k,n;
4	printf("Enter the number of terms: ");
5	scanf("%d",&n);
6	for(k = 1;k <= n;k++) {
7		t = i + j;
8		printf("%d,",t);
9		if(t == 0 && n >= 2) {
10			t = 1;
11			printf("%d,",t);
12			k++;
13		}
14		i = j;
15		j = t;
16	}
17	return 0;
18}

Output

Enter the number of terms: 30
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,