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);
   }

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);
}

thanks for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.