57 lines
1.1 KiB
Plaintext
57 lines
1.1 KiB
Plaintext
|
/*
|
|||
|
file name: fibUsingRecursive.c
|
|||
|
description: Fibonacci numbers
|
|||
|
<20>Q<EFBFBD>Ψ<EFBFBD><CEA8>ƻ<EFBFBD><C6BB>j<EFBFBD>I<EFBFBD>s<EFBFBD><73><EFBFBD>O<EFBFBD><4F><EFBFBD>ƦC<C6A6>p<EFBFBD><70>
|
|||
|
|
|||
|
<20>O<EFBFBD><4F><EFBFBD>ƦC<C6A6><43>0,1,1,2,3,5,8,12,21,<2C>K
|
|||
|
<20>䤤<EFBFBD>Y<EFBFBD>@<40><><EFBFBD><EFBFBD><EFBFBD>e<EFBFBD>G<EFBFBD><47><EFBFBD><EFBFBD><EFBFBD>M,<2C>B<EFBFBD><42>0<EFBFBD><30><EFBFBD><EFBFBD>0,<2C><>1<EFBFBD><31><EFBFBD><EFBFBD>1
|
|||
|
*/
|
|||
|
|
|||
|
#include <stdio.h>
|
|||
|
#include <ctype.h>
|
|||
|
#include <stdlib.h>
|
|||
|
|
|||
|
/* <20><><EFBFBD>ƭ쫬<C6AD>ŧi */
|
|||
|
long Fibonacci(long);
|
|||
|
void flushBuffer(void);
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
char ch;
|
|||
|
long n;
|
|||
|
|
|||
|
printf("-----Fibonacii numbers Using Recursive-----");
|
|||
|
do {
|
|||
|
printf("\nEnter a number(n >= 0): ");
|
|||
|
scanf("%ld", &n);
|
|||
|
flushBuffer();
|
|||
|
/* n <20>Ȥj<C8A4><6A>0 */
|
|||
|
if (n < 0)
|
|||
|
printf("Number must be > 0\n");
|
|||
|
else
|
|||
|
printf("Fibonacci(%ld) = %ld\n", n, Fibonacci(n));
|
|||
|
printf("Contiune(y/n)? ");
|
|||
|
ch = toupper(getchar());
|
|||
|
} while (ch == 'Y');
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
/* <20>Q<EFBFBD>λ<EFBFBD><CEBB>j<EFBFBD><6A><EFBFBD>ƩI<C6A9>s<EFBFBD><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƨӭp<D3AD><70>Fibonacci numbers */
|
|||
|
long Fibonacci(long n)
|
|||
|
{
|
|||
|
if (n == 0) /* <20><> 0 <20><><EFBFBD><EFBFBD> 0 */
|
|||
|
return 0;
|
|||
|
else if (n == 1) /* <20><> 1 <20><><EFBFBD><EFBFBD> 1 */
|
|||
|
return 1;
|
|||
|
else /* <20><><EFBFBD>j<EFBFBD>I<EFBFBD>s<EFBFBD><73><EFBFBD>Ʋ<EFBFBD> N <20><><EFBFBD><EFBFBD> n-1 <20><> n-2 <20><><EFBFBD><EFBFBD><EFBFBD>M */
|
|||
|
return(Fibonacci(n-1) + Fibonacci(n-2));
|
|||
|
}
|
|||
|
|
|||
|
void flushBuffer()
|
|||
|
{
|
|||
|
while (getchar() != '\n') {
|
|||
|
continue;
|
|||
|
}
|
|||
|
}
|