473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Fibonacci with recursion

13
hey..
can u help me wid this..
#include<stdio.h>
#include<conio.h>
void main()
{
int fib(int);
int n,f=1,ans;
clrscr();
while(f==1)
{
printf("\n***Generating A Fibonacci Series***");
printf("\nEnter Number Of Terms Required: ");
scanf("%d",&n);
if(n<=1)
{
printf("\nInvalid Choice..Number Of Terms Should Be more Than 1");
printf("\nDo You Want To Try Again...\n1.Yes\n2.No");
scanf("%d",&f);
}
else
{
printf("\nSeries: %d",fib(n));
f=2;
}
}
getch();
}
int fib(int a)
{
if((a==0)||(a==1))
{
return(1);
}
else
{
return(fib(a-1)+fib(a-2));
}
}
i wat to print d numbers as well...eg if n =5
1 1 2 3 5
Feb 19 '09 #1
14 2216
newb16
687 512MB
@Tired
You what to what?

void fibfib(int n)
{
int q;
if(q=n-1)fibfib(q);printf("%d ",fib(q));
}
Feb 19 '09 #2
Tired
13
wat=*want...
umm i dint understand the func u hv typed..can u help me with understanding it???it wud b great if u can..

thnx..
Feb 20 '09 #3
JosAH
11,448 Expert 8TB
@Tired
Please spell your words properly; this is an international forum not your local text speak community.

kind regards,

Jos
Feb 20 '09 #4
Tired
13
I am sorry...but can u please help me understand this...
void fibfib(int n)
{
int q;
if(q=n-1)fibfib(q);printf("%d ",fib(q));
}
Feb 21 '09 #5
JosAH
11,448 Expert 8TB
@Tired
Note that fib() is your function; the function fibfib() is just defined for printing the return values of your function. Try it by hand for small values of n. Also note that the expression q=n-1 is non-zero when n != 1

kind regards,

Jos
Feb 21 '09 #6
Tired
13
But its not printing all the values(or the entire series). Its just printing the final answer.Where should i call this function?in the main block before i am printing my answer?
And most of the times its not printing any thing.
Feb 22 '09 #7
JosAH
11,448 Expert 8TB
@Tired
How strange; to be sure I ran it as well and it runs fine, i.e. it prints out the first n values of the standard Fibonacci series. Can you show us some code?

kind regards,

Jos
Feb 22 '09 #8
Tired
13
#include<stdio.h>
#include<conio.h>
void main()
{
int fib(int);
void fibfib(int);
int n,f=1,ans;
clrscr();
while(f==1)
{
printf("\n***Generating A Fibonacci Series***");
printf("\nEnter Number Of Terms Required: ");
scanf("%d",&n);
if(n<=1)
{
printf("\nInvalid Choice..Number Of Terms Should Be more Than 1");
printf("\nDo You Want To Try Again...\n1.Yes\n2.No");
scanf("%d",&f);
}
else
{
fibfib(n);
printf("\nAns i.e. term no. %d = %d",n,fib(n));
printf("\nDo You Want To Continue:\n1.Yes\n2.No\n");
scanf("%d",&f);
}
}
}
int fib(int a)
{
if((a==0)||(a==1))
return(1);
else
return(fib(a-1)+fib(a-2));
}
void fibfib(int a)
{
int q;
if(q=a-1)
fibfib(q);
else
printf("%d ",fib(q));
}

this is the code i tried.....
Feb 22 '09 #9
JosAH
11,448 Expert 8TB
@Tired
I don't see an 'else' in the original code ...

kind regards,

Jos
Feb 22 '09 #10
Tired
13
it doesnt work without else also!!!i tried..so then is that last print statement not a part of the for loop??and is the way i am calling the function right??
Feb 23 '09 #11
JosAH
11,448 Expert 8TB
@Tired
Don't shout; there is no need to start a "yes it does, not it doesn't" discussion. You have to show or prove that it doesn't work; at least show the code you used. I also ran it and it worked for me (both in C and C++, my Java version also ran correctly).

kind regards,

Jos
Feb 23 '09 #12
i have tried ur code..no problem with that.
But i would like to advise u that without detecting if the user want to continue by pressing 1 or Y or 2 or N.Use isdigit to differentiate them else if when alphabet is pressed the loop will keep on going
Feb 27 '09 #13
Tired
13
hey khaichiew85,
so u mean that the "fibfib" func is displaying the series for u?can u help with the calling statement and evrything??

regards
Amy
Feb 28 '09 #14
JosAH
11,448 Expert 8TB
@Tired
There is no need for help: I simply copied/pasted that fibfib function, crafted a loop around it and voila. Why don't you show your code? We are not going to spoonfeed you complete programs.

kind regards,

Jos (moderator)
Feb 28 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Brett Trost | last post by:
OK, I wanted to make a recursive fibonacci method in Perl, and I can't understand why it is not working, especially since I wrote the exact same thing in Java and it works. Here's the perl code: ...
28
by: dleecurt | last post by:
Hello, I have a small problem, I am trying to write a program that will calculate the Fibonacci number series, and I have the code complete with one problem. I used a long in to store the numbers,...
11
by: MARQUITOS51 | last post by:
Hey guys this is the fibonacci series. But Im having this huge problem. First of all I want to do the simplest code possible so then I can use user defined functions and arrays. But this one didnt...
14
by: felixnielsen | last post by:
Im actually kinda embarassed to ask this question... @code start #include <iostream> int main() { unsigned long long a = 1; unsigned long long b = 1; for (int i = 0; i < 45; i++) { a += b;...
3
by: greek | last post by:
Hi! I hav to generate fibonaaci series using recursion: 0,1,1,2,3,5,8,18,21... whr fibonacci(0)=0 fibonacci(1)=1 fibonacci(n)=fibonacci(n-1)+fibonacci(n-2) ive witten the code but having 2...
2
by: veeru | last post by:
Hi All, Can anyone tell about how to create a FIBONACCI series in VB.Net and C# Thanks in Advance, Veeru
17
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
2
by: guneet bhatia | last post by:
please help with fibonacci series using recursion..
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.