hi
I need help with this problem. I have tried writting the program, i just can not get it to run properly.
Please Write
The Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
(a) Write a nonrecursive function fibonacci( n ) that calculates the nth Fibonacci number.
and than you
22 123005 Banfa 9,065
Recognized Expert Moderator Expert
This is very simply achieved with a loop, you should have a go yourself and then we will help if there are problems in your solution.
There is a formula as well which you should be able to find with a few minutes web search. However it uses the floating point calculations on an irrational number so may not be entirely suited to a computer (where floating point calculations are not exact).
D_C 293
Contributor
There is a simple recurrence relation which will help.
fibonacci(n) = fibonacci(n1) + fibonacci(n2). That's the recurrence relation which is recursive, but it is easily implemented as a loop.
//Non recursive method 
#include<stdio.h>

#include<conio.h>


void main()

{

int initial_value=0,final_value=1,temp_value,count=10;

// count contains the number of elements to be generated.....

for(count=1;count<=10;count++)

{

temp_value=initial_value+final_value;

printf("\n%d",initial_value);

initial_value=final_value;

final_value=temp_value;

}


getch();

}
 // Recursive Method.......


int fibo_gen(int,int,int);


void main()


{


int initial_value=0,final_value=1,count=100;

fibo_gen(initial_value,final_value,count);

getch();


}


int fibo_gen(int initial_value,int final_value,int count)


{


int temp;


if (count>0)


{


printf("\n%d",initial_value);

temp=initial_value+final_value;

initial_value=final_value;

final_value=temp;

count=count1;

fibo_gen(a,b,c);


}


else

return(0);


}
]
 main()

{

int n,fib[20];

printf("enter the value of n");

scanf("%d",&n);fib[0]=0;fib[1]=1;

for(i=2;i<n;i++)

fib(i)=fib(i1)+fib(i2);
Banfa 9,065
Recognized Expert Moderator Expert
main()
{
int n,fib[20];
printf("enter the value of n");
scanf("%d",&n); fib[0]=0;fib[1]=1;
for(i=2;i<n;i++ )
fib(i)=fib(i1)+fib(i2);
You haven't output the result,
you haven't finished the program, this wont compile
if the user inputs a value >= 20 then this program writes off the end of the fib array invoking undefined behaviour.
Apart from that a very good solution :D
G'day
where error?
1 error(s), 0 warning(s)  # include <iostream.h>

int fibonacci(int n)

{

int x1 = 0, fib;

int x2 = 1;

if(n >= 1)

{

for(int i=2;i<= n; i++)

{

fib = x1+ x2;

x1 = x2;

x2 = fib;

}


return fib;

}
D_C 293
Contributor
You only return a value if n > 0. Afterwards, you should add "return 1; }" assuming n is nonnegative. Also, usually the sequence starts with 1 and 1, not 0 and 1. You may be off by one term.
#include<iostre am>
using namespace std; 
int fibonacci(int n)

{

int x1 = 0, fib;

int x2 = 1;

if(n >= 1)

{

for(int i=2;i<= n; i++)

{

fib = x1+ x2;

x1 = x2;

x2 = fib;

}

}


return fib;

}

int main(){

for(int i=2;i<=10;i++) {

cout<<fibonacci(i)<<endl;

}

return 0;

}
this is ok now I'm finish it
but I need this
(1) Determine the largest int Fibonacci number that can be printed on your system.
(2) Modify the program of part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified program to repeat part (b).
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics 
by: YS Sze 
last post by:
If you know the exact longitude and latitude for a specific
location, would anyone think it'd make any sense to find out if this
set of location numbers is really part of the Fibonacci series or not?
Or, another way to look at this is that: Would anyone of you think it
is worth a while to find out if there is any location on earth with
the set of longitude and latitude numbers that coincides with the
Fibonacci series?
As I see it, if...

by: CII 
last post by:
Hi everybody. I've been reading posts a year old about the fibonacci
series right on this newsgroup, and while it's not directly games
related, I'll share my own notes as well.
On another newsgroup there is an ongoing discussion about the famous
fibonacci sequences, and a fine program written by a poster got my
attention so just for fun I wrote one myself and put it on the web just
about two days ago. It can be found on the site
...

by: student 
last post by:
Hi guys,
please tell me how to write the iterative version of fibonacci series using a stack.
Thanks

by: srinpraveen 
last post by:
I know to write a program to print the fibonacci series. But the problem is my teacher has asked us to write a program to print the natural numbers that are not involved in the fibonacci series.
For example if the user gives 7 terms of the series to be displayed,
then the display of the fibonacci series is 0, 1,1, 2, 3, 5, 8.
But the natural numbers not involved are 4, 6 and 7. That's what my teacher wants.
But I am struggling to write a...

by: guneet bhatia 
last post by:
please help with fibonacci series using recursion..
 
by: rajeshsuri 
last post by:
hi this is rajesh please send program for fibonacci series using pointers or functions

by: omran11 
last post by:
how to generate fibonacci series (lesser than 100)

by: marktang 
last post by:
ONU (Optical Network Unit) is one of the key components for providing highspeed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...

by: Oralloy 
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bitfields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++12 std=c++20 Wnarrowing bit_field.cpp
Here is the code in...

by: jinu1996 
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...

by: Hystou 
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
 
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, ZWave, WiFi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...

by: agi2029 
last post by:
Let's talk about the concept of autonomous AI software engineers and nocode agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...

by: adsilva 
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

by: muto222 
last post by:
How can i add a mobile payment intergratation into php mysql website.

by: bsmnconsultancy 
last post by:
In today's digital era, a welldesigned website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
 