Fibonacci numbers c++

For my computer programming course I need to write a program that asks the user how many numbers to display and then displays that many Fibonacci numbers. The program should validate the input.
Examples:
How many Fibonacci numbers do you want to display? 5
0,1,1,2,3

How many Fibonacci numbers do you want to display? 0
The program cannot display 0 Fibonacci numbers.

Please help me get started. We have just started learning about loops and files. So I think it should use loops, but I do not know how?
Last edited on
How bout something along the lines of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
short num1(0), num2(1), temp;
cout << "Enter number of numbers you wish displayed: "
short iterations;
cin >> iterations;
if(iterations > 0)
{
cout << num1;
iterations--;
}
if(iterations > 0)
{
cout << num2;
iterations
}
while(iterations > 0)
{
temp = num2;
num2 = num1 + num2;
num1 = temp;
iterations--;
}


I haven't tested it but it should go something like what I said above. There's probably ways to make the code neater too.
line 13 should be iterations--; in my previous post.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream.h>
#include <stdlib.h>

void main(){
	int MaxNum; //Maximal Fibonacci numbers
	unsigned long int Fibonacci[1000]; //Array of Fibonacci numbers
	Fibonacci[0]=0; //1st Fibonacci number
	Fibonacci[1]=1; //2nd Fibonacci number
	Fibonacci[2]=1; //3rd Fibonacci number

	cout<<"How many Fibonacci numbers do you want to display? ";
	cin>>MaxNum;

	if(MaxNum==1){ //Avoid 'Array Overflow'
		cout<<Fibonacci[0]<<endl;
	}else if(MaxNum==2){ //Avoid 'Array Overflow'
		cout<<Fibonacci[0]<<","<<Fibonacci[1]<<endl;
	}else if(MaxNum>2){ //Avoid 'Array Overflow'
		for(int n=1; n<=MaxNum; n++){
			Fibonacci[n]=Fibonacci[n-1]+Fibonacci[n-2]; //Calculate a Fibonacci number
			cout<<Fibonacci[n-1];
			if((n%6)==0)
				cout<<endl;
			if( (n==MaxNum) || (((n%6)==0)) )
				goto CrossComma; //Don't print ','
			cout<<",";
CrossComma:;
		}
		cout<<endl;
	}else{
		cout<<"The program cannot display 0 Fibonacci numbers."<<endl;
	}
	system("pause");
}
OK, I know it's not a good idea to give complete solutions, but just this once! I am fickle and I dont like complex looking main's, but I also realize that you may not have done classes yet, so if this solution is overkill then forgive me, and just keep it till later (or submit it anyway for possible extra brownie points). Here is the main().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include"Fibo.h"

using namespace std;

int main(int argc, char* argv[])
{
	int numFibs = 0;

	Fibo fibGen;
	
	// Ask user for number of Fibs reqd.
	cout << "How many Fibonacci numbers do you want to display? ";
	cin >> numFibs;

	if (numFibs < 1)
	{
		// Trying to compute 0 or less than 0 Fibs has NO meaning.
		cout << "The program cannot display 0 Fibonacci numbers." << endl;
	}
	else
	{
		// OK - let's do it!

		// Unless you are a mathematician doing Number Theory research the need for more than 70
		// Fibs is a mute point, the idea is to show that you can infact generate Fibo numbers!
		if(numFibs > 70) numFibs = 70;

		for(int i=0; i<numFibs; ++i)
		{
			cout << fibGen.next();
			if (i<numFibs-1) cout << ","; 
		}
	}
}


Here is the header file:
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once

class Fibo
{
public:
	Fibo(void);
	~Fibo(void);

	unsigned long long next(void);
private:
	unsigned long long a[4];
};


And, last but not least the member functions for the class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Fibo.h"

Fibo::Fibo(void)
{
	a[0] = a[1] = 0;
	a[2] = a[3] = 1;
}

Fibo::~Fibo(void)
{
}

unsigned long long Fibo::next(void)
{
	a[0] = a[1];
	a[1] = a[2];
	a[2] = a[3];
	a[3] = a[1] + a[2];
	return a[0];
}


Like I said - probably overkill, but it gives you a nice looking main() :-p

I also noted that you you dont have a requirement for an upper limit for the number of Fibs, so I set a ceiling of 70. Requests for more than 70 are just set to 70, a comment in the code says why. Enjoy!
Last edited on
I am unsure how to do the ways posted. I tried doing this and this is what I got. Can anyone point me in the right direction?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Exercise 1 PS6
//Dylan Metz

#include <iostream.h>

int main()
{
int num;
int count=1;
int fib1=0;
int fib2=1;

cout << "How many Fibonacci #'s do you want to display? ";
cin>>num;

while (num<1 || num>70)
{
cout << "Please enter a number in the range of 1-70.";
cin>>num;
}
while (num>=1 && num<=70)
{
if (num==1)
{
cout<<fib1;
}
else if (num==2)
{
cout<<fib2;
}
else
{
cout<<fib1+fib2;
}
count++;
}

return 0;
}


This is what the program needs to do:

user input:
How many Fibonacci #'s do you want to display?

Example 5:

output:

0,1,1,2,3


The input needs to be >= to 1

and needs the output to look like this:
0,1,1,2,3
saurabh qupta, what you posted was almost exactly what I needed. Thank-you!

I am curious if i can get the same result using #include <iostream>? The course i'm taking is an introduction to C++ programing and the only #include we have went over are iostream.h, math.h, and iomanip.h.

I need to use:
cout

cin

and a loop.

But thank-you very much. I really appercaite your advice.

Also if it helps the book we are using is Starting out with C++: from control structures through objects. By Tony Gaddis.

We are on CH5
Last edited on
Okay i think i have working code.

The only problem is I need to add spaces and a , after each number.

This is the out put of get:

#=10

output: 0112358132134

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Exercise 1 PS6
//Dylan Metz

#include <iostream.h>

int main()
{
int num;
int count=1;
int fib1=0;
int fib2=1;
int fib0;

cout << "How many Fibonacci #'s do you want to display? ";
cin>>num;

while (num<1 || num>70)
{
cout << "Please enter a number in the range of 1-70.";
cin>>num;
}
while (count<=num)
{
if (count==1)
{
cout<<fib1;
}
else if (count==2)
{
cout<<fib2;
}
else
{
fib0=fib1+fib2;
cout<<fib0;
fib1=fib2;
fib2=fib0;
}
count++;
}

return 0;
}


Also how can i save the output to a text document?
Last edited on
Never mind. This should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//Exercise 1 PS6
//Dylan Metz

#include <iostream.h>
#include <fstream.h>
#include <string.h>

int main()
{
int num;
int count=1;
int fib1=0;
int fib2=1;
int fib0;
int tem;
char comma=',';

cout << "How many Fibonacci #'s do you want to display? ";
cin>>num;

while (num<1 || num>70)
{
cout << "Please enter a number in the range of 1-70.";
cin>>num;
}
while (count<=num)
{
if (count==1)
{
cout<<fib1;
}
else if (count==2)
{
cout<<comma<<fib2;
}
else
{
fib0=fib1+fib2;
cout<<comma<<fib0;
fib1=fib2;
fib2=fib0;





}
count++;
}
Hahaha, no-one ever gave me full answers for my homework assignments when I first started :P

Anyway, no the code you stated above will not work. It is also very messy. I don't understand why you felt the need to do this:

1
2
#include <fstream.h>
#include <string.h> 


This code is much cleaner and will do everything you stated in the question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

int main()
{
    int num;
    
    cout<<"How many Fibonacci #'s do you want to display?";
    cin>>num;
    
    if (num>=1 && num <= 70)
    {
    int a = 0;
    int b = 1;
    int sum;
    
    for (int i=0; i<num; i++)
    {
        cout<<a<<", ";
        sum=a+b;
        a=b;
        b=sum;
    }
    }
    
    else
    cout<<"Please enter a number in the range of 1-70.";


return 0;
}


EDIT: If you want to write this output to a file, you will have to do this:
(I also realized that there was a problem with the comma being printed 1 more time than it should be at the end and fixed it in this code)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int num;
    ofstream myfile;
    
    cout<<"How many Fibonacci #'s do you want to display?";
    cin>>num;
    
    if (num>=1 && num <= 70)
    {
    int a = 0;
    int b = 1;
    int sum;
    
    myfile.open ("output.txt");
    
    for (int i=0; i<num; i++)
    {
        cout<<a;
        myfile<<a;
        if(i<num-1)
        {
                   cout<<", ";
                   myfile<<", ";
        }
        sum=a+b;
        a=b;
        b=sum;
    }
    }
    
    else
    cout<<"Please enter a number in the range of 1-70.";
    
    myfile.close();


return 0;
}
Last edited on
Topic archived. No new replies allowed.