[c언어] 피보나치 수열

2020. 12. 16. 10:51Computer Science/C

 

피보나치 수열을 출력하는 프로그램을 작성한다.

 

#include <stdio.h>
void printFibo(int n);
int main(void)
{
	int num, i;

	printf("몇개의 피보나치 수열값을 출력할까요? (3보다 큰 정수):");
	scanf_s("%d", &num);

	for (i = 0;i < num;i++)
		printFibo(i);

	printf("\n");
}

void printFibo(int n)
{
	int j, a=1,b=1,c=0;
	if (n == 0 || n == 1)
		printf("1 ");
	else
	{
		for (j = 1;j < n;j++)
		{
			c = a + b;
			a = b;
			b = c;
		}
		printf("%d ", c);
	}
	return;
}

결과

 

'Computer Science > C' 카테고리의 다른 글

[c언어] 사이클 숫자 출력  (0) 2020.12.16
[c언어] 5단위 합 출력  (0) 2020.12.16
[c언어] 중첩 반복문 연습  (0) 2020.12.16
[c언어] 소수판별  (0) 2020.12.16
[c언어] 구구단 출력  (0) 2020.12.16