Fibonacci numbers in C, Objective-C
Example for versions
gcc 3.4.5,
gcc 3.4.5 (Objective-C),
tcc 0.9.25
This example uses recursive definition of Fibonacci numbers. Note the difference from C++ example: loop counter must be declared outside of the loop, and printf
is used for output instead of std::cout
.
#include <stdio.h>
int fibonacci(int n)
{
return ( n<=2 ? 1 : fibonacci(n-1) + fibonacci(n-2) );
}
int main(void)
{
int n;
for (n=1; n<=16; n++)
printf("%d, ", fibonacci(n));
printf("...\n");
return 0;
}
Comments
]]>blog comments powered by Disqus
]]>