Hello,

I am trying to do a recursive function using fiboncci. I have been working on this and it is not working.

#include "stdafx.h"
#include "stdio.h"

int fib (int n); // function prototype//

int main (void)

{
    int num, result;
    printf ("Please enter a positive integer\n"); 
    scanf ("%d", &num);
    result= fib (num);

    return (0);
}
int fib (int n)
{
    int   old_number= 0;
    int   current_number = 1;
    int   next_number;

}
   if ((n == 0) || (n == 1))      // stopping cases
      return 1;
   else                           // recursive case
      return fib(n - 1) + fib(n - 2);
   }

Edited 3 Years Ago by happygeek: fixed formatting

I don't see a problem. You don't need this piece of code:

{
int old_number= 0;
int current_number = 1;
int next_number;

}

Hi there,
Other than the syntax error, there is no issue with the logic... Below I am pasting the correct code with a print statement...

#include "conio.h"
#include "stdio.h"

int fib (int n); // function prototype//

int main (void)

{
	int num, result;
	printf ("Please enter a positive integer\n");
	scanf ("%d", &num);
	result= fib (num);
	printf("\n Fib : %d",result);
	getch();
	return (0);
}
int fib (int n)
{

	if ((n == 0) || (n == 1)) // stopping cases
	return 1;
	else // recursive case
	return fib(n-1) + fib(n - 2);
}
This question has already been answered. Start a new discussion instead.