A recursive function for fibonacci with command-line args.
fib3.c
/* File: fib3.c
*
* A recursive function for fibonacci with
* command-line args.
*/
#include
int Fib (int n, int depth);
void Indent (int depth);
main (int argc, char *argv[])
{
int n;
/* check that (1) only one arg
(2) which is an integer and
(3) non-negative.
*/
if (argc == 2 && 1 == sscanf(argv[1], "%d", &n) && n > -1)
{
Fib (n, 0);
}
else
{
printf("Usage: Fib \n");
}
}
int Fib (int n, int depth)
{
int result;
Indent (depth);
printf("fib(%d)\n", n);
if (n < 2)
{
result = 1;
}
else
{
result = Fib (n - 1, depth + 1) +
Fib (n - 2, depth + 1);
}
Indent (depth);
printf("%d\n", result);
return (result);
}
void Indent (int depth)
{
int i;
for (i = 0; i < depth; i++)
{
printf("| ");
}
}