Thread: can someone please tell me what is wrong with the code

1. can someone please tell me what is wrong with the code

a program to print n terms of Fibonacci series using recursion.
the problem is that when am executing the code the loop is not stopping where it should...
Code:
```#include<stdio.h>
#include<conio.h>

void recurring_fibonaucci(int ,long int ,long int );
int main()
{
int len;
printf("\t\t\tFibonaucci series\n\nlength of the series should be between 1 to 47.");
do {
printf("\nenter the length of series :");
scanf("%d",&len);
printf("\n");
}while(!(len>1&&len<47));//length should be between 1 and 47

printf("\n\ngenerated number(s) via Recurion\n");
long int r_m=0,r_n=1;
printf("%ld\t",r_m);
recurring_fibonaucci(len ,r_m ,r_n);

getch();
return 0;
}

void recurring_fibonaucci(int len,long int r_m,long int r_n)  {

//    printf("\nlength: %d\n\n",len);
while(len>1) {
printf("%ld\t",r_n);
len=len-1;
recurring_fibonaucci(len ,r_n ,r_m+r_n);
}
}```

2. Well... how about some hint of what you expect and what you're getting?

3. fibonaucci series: 0, 1, 1, 2, 3, 5, 8, 13,.....i.e the first no. is 0 and the second no is 1 and after that the nth digit is the sum of (n-1)&(n-2).
so in the program if the input is 6,i.e the length of series is 6 then the series should be ---> 0, 1, 1, 2, 3, 5
but obviously, something is wrong

4. You're mixing iteration with recursion. What you should be doing is choosing either iteration or recursion.

But you still didn't answer CommonTater's questions:

* what do you expect
* what do you get

5. Originally Posted by MWAAAHAAA
You're mixing iteration with recursion. What you should be doing is choosing either iteration or recursion.
Actually... recursion should be a last resort thing for when there's just no other way. It's on the "no no" list right behind "Self-modifying code".

6. the prog. works...i suppose it only recurion and not a mix
Code:
```//Write a program to print n terms of Fibonacci series using recursion
#include<stdio.h>

int recurring_fibonaucci(int ,long int ,long int );
int main()
{
int len;
printf("\t\t\tFibonaucci series\n\nlength of the series should be between 1 to 47.");
do {
printf("\nenter the length of series :");
scanf("%d",&len);
printf("\n");
}while(!(len>1&&len<47));//length should be between 1 and 47

printf("\n\ngenerated number(s) via Recurion\n");
long int r_m=0,r_n=1;
printf("%ld",r_m);
recurring_fibonaucci(len-1 ,r_m ,r_n);

getch();
return 0;
}

int recurring_fibonaucci(int len,long int r_m,long int r_n)  {
//int r_l = len;
//printf("\nlength: %d\n\n",len);
printf("\t%ld",r_n);
if(len>1)
recurring_fibonaucci(len-1 ,r_n ,r_m+r_n);
else
return 0;
};```

7. all rite...

8. Originally Posted by CommonTater
Actually... recursion should be a last resort thing for when there's just no other way. It's on the "no no" list right behind "Self-modifying code".
As pointed out in another thread, the matter is not as black-and-white when practical considerations are taken into account.