C language realizes outputting the first n items of Fibonacci sequence

Baidu Encyclopedia: Fibonacci sequence (Fibonacci sequence), also known as the golden section sequence, was introduced by the mathematician Leonardo Fibonacci (Leonardo Fibonacci) with the example of rabbit breeding, so it is also called "rabbit sequence". ", refers to such a series of numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Problem-solving ideas: the cumulative sum of the first two is stored in the third variable, and the loop is used to output The source code of each item
is as follows:
#include<stdio.h>
int main()
{ int f = 1;//the first Fibonacci number int s = 1;//the second Fibonacci number int i = 0;// int c = 0; int n = 0;//Output the Fibonacci sequence of the first n items scanf_s("%d", &n); for ( i =3 ; i <= n; i++) //1 and 1 are printed directly so i is calculated from 3 { if (f == 1 && s == 1) { printf("%d %d ", f, s);










	}

	c = f + s;
	s = f;
	f = c;
	printf("%d ", c);

}
return 0;

}

Guess you like

Origin blog.csdn.net/Kirihara_Yukiho/article/details/123183946