Program to display fibonacci series in C

Source Code

// 19. Program to display fibonacci series
// Arpan Das - CST 2nd Year (VOCLET)

// fibonacci series using while loop
// 0,1,1,2,3,5,8,13,21...N-th term
// x,y
//   x,y
//	   x,y
// i	x 	y  	r
// 1	0	1	0
// 2 	1 	1  	1  x=y, y=x+y
// 3  	1 	2  	1
// 4  	2 	3  	2
// 5  	3 	5  	3
#include <stdio.h>
//#include <conio.h>

int main() {
	int i=1, x=0, y=1, z, n;
	//clrscr();

	printf("Please enter the value of N: ");
	scanf("%d", &n);
	while (i<=n) {
		if (i!=1) {
			z = x;
			x = y;
			y = z+y;
		}
		printf("%d, ", x);
		i++;
	}
	printf("\b\b ");

	//getch();
}

/*
// by Subhankar Sir
void main() {
	int x=0,y=1,s,i=3;
	clrscr();
	printf("%d,%d",x,y);
	while(i<=13) {
		s=x+y;
		printf(", %d",s);
		x=y;
		y=s;
		i++;
	}
	getch();
}*/

Output

Please enter the value of N: 15
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377

Leave a Reply

Note: Your email address will not be published. Required fields are marked *