954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I need help writing a program using Fibonacci sequence

The program needs to ask for n and then output the first n terms of the Fibonacci sequence.

exp.
n = 6
1, 1, 2, 3, 5, 8
1+1 =2
2+1=3
3+2=5
5+3=8

My origional program used a nested for loop.
I am not sure if that is right to use in this type a situation.
I would like suggestions to make it work.

#include

using namespace std;
int main ()
{
int fOne = 1;
int fTwo = 1;
int fThree = 2;
long fN;
long fNN;

cout << "How many n terms do you want in the sequence: ";
cin >> fN;

for ( fN = 1 ; fN >= 3 ; fN++ )
{
for (fNN = 1; fNN >= fN; fNN++)
fNN = (fN - 1) + (fN - 2);
cout << fN;
}
}

gpleonar
Newbie Poster
2 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

You have some mistakes in this code:
cin >> fN;

for ( fN = 1 ; fN >= 3 ; fN++ )
As you introducefN from the standard input, why are you initializing it in the for loop? More, if you are initializing fN = 1, then the condition in for loop fN >= 3 is never accomplished.

Then, for the Fibonacci series, as I know, every term of the series, starting with the third term, is the sum of the 2 preceding terms. In a vector, this should be a[n] = a[n-1] + a[n-2], right?
Or, in the for loop

for (fNN = 1; fNN >= fN; fNN++)
    fNN = (fN - 1) + (fN - 2);
cout << fN;

you are adding the same term, subtracting from it first 1, then 2..this is not exactly what you want...

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

Here you have some sample code:

#include <iostream> 

using namespace std;
int main ()
{
int a[128];
int fN;
int i;

a[0]=1;
a[1]=1;

cout << "How many n terms do you want in the sequence: ";
cin >> i;

for ( fN = 2 ; fN <= i ; fN++ )
{
a[fN]=a[fN-1]+a[fN-2];
cout << a[fN] <<"\n";
}
}

It has some limitations (you can't enter a value >128, for example), but I hope you get the idea.

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

Okay, when I ran this program, I saw one error:
1. I did not see the first two ones in the Fibonacci sequence.

Also, can you tell me that the a and the i means in this program??

naya22
Light Poster
48 posts since Apr 2007
Reputation Points: 24
Solved Threads: 0
 

>1. I did not see the first two ones in the Fibonacci sequence.
Probably because those values weren't printed. They're in the generated sequence though. frrossk's program kind of sucks anyway.

>Also, can you tell me that the a and the i means in this program??
a is an array that holds each number in the sequence and i is the expected length of the sequence.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

> Nov 3rd 2004
> 2 Minutes Ago
Lets see, 2 and a half years.
Now, what was your question again?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

I don't see a problem resurrecting the thread in this case.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

hey i have this code..... n it works


# include
void main ()
{
int x=0, y=1, b, n=0,ter;

cout<<"Enter The number of terms";
cin>>ter;
cout<

DrAgon PrinceSS
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

you do realize this thread is almost 6-years old... the OP has probably completely forgotten about it by now.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

LOL. wht can i say .. i just started c++ and made this prog today.!

DrAgon PrinceSS
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 
LOL. wht can i say .. i just started c++ and made this prog today.!


Say you're sorry, don't do it again, andRead the Member Rules!!! Violating the rules will get you banned from the site.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

thank you princess it realy worked

harshmore
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You