0

This is my recursive code that will print numbers according to fibbonacci series..
My problem is I want the program to stack (store the numbers) numbers before the printing (in order starting with 3-21) but it should still be a recursive program.
Can you help me, which part I should edit?
I want it to stack before it prints. What this code do is it prints the number immediately..

#include <stdio.h>

void fibbo ();

main (){
int cnt=5, n1=1, n2=2;
clrscr ();
fibbo (cnt,n1,n2);
return 0;
getch ();
}

void fibbo (int cnt, int n1, int n2) {
if (cnt==1) {
printf ("%d", n1+n2);
}
else {
printf ("%d", n1+n2);
fibbo (cnt-1, n2, n1+n2);
}

}
2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by deepugtm
0
#include<stdio.h>
#include<conio.h>
#define F_NO 5
int fab(int);
void main()
{
int i,a[F_NO];
clrscr();
for(i=0;i<F_NO;i++){
/*printf("%d\t",fab(i));*/
a[i] = fab(i);
}
getch();
}
int fab(int n)
{
int a;
if(n<2)
return(1);
else
return(fab(n-1)+fab(n-2));
}

This Program will store the first five fibbonacci numbers into array a. You can change the no. of the fibonacci numbers to be stored.

0

Hi.. thank you for replying to this thread..
Btw, I mean stacking the numbers.. not store them in an array..

Example of a program with stacking
Prints numbers from 1 - 5

#include <stdio.h>
int show ();

main () {
show (5);
getch ();
}

show (x) {
if (x==1)  {
printf ("%d", x);
}
else {
show (x-1);          /*The part where the program actually stacks the numbers*/
printf ("%d", x);
}
}

The output 12345

0

I think, By replacing those return statements with printing statement will solve your problem.

This topic has been dead for over six months. 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.