0

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 by happygeek: fixed formatting

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by rrreeefff
0

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;

}
0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.