C++ prints all the Fibonacci numbers that lie between the two integers entered.

Hi guys,
how can I modify this code so it does the following.Reads two integers from the user and then prints all the Fibonacci numbers that lie between the two integers entered. Also, print how many numbers were printed and how many of them were odd. Using only local variables. Thanks in advance.


for Example:
Enter low and high limits: 50 500
55 89 144 233 377
4 of the 5 numbers printed were odd.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
int num=1;
int anterior=0;
int aux;

for (int i=0; i <20;i++)
{
  cout << num << ' ';
  aux=num;
  num+=anterior;
 anterior=aux;   

}



return 0;
}
Hi guys ,


I forgot to mention I can only use loops, and no arrays and no functions. I'm trying to learn
the basics of assembly language using pep8 virtual machine, and I'm having trouble writing the whole program mention above. I figure if I put it in c++ then I can just translate it to assembly language.
I can only use loops, and no arrays and no functions

so take out the code from the countFibs() function in the link above and stick it into main(), the one function at the very least you have to use for C++
You basically have all the code you need.
First, you'd need to find the smallest Fibonacci number that is greater than or equal to your lower limit. This can be done via modifying your for loop, like so
1
2
3
4
5
6
7
while( num < lower_limit ) {
    //cout << num << ' ';
    
    aux = num;
    num += anterior;
    anterior = aux;
}


Use the same logic above to print the Fibonacci numbers up to your upper limit.

After you've done that, you can just use a counter variable to count how many Fibonacci numbers are between your limits, and a simple if statement to check if the current number is odd.
1
2
3
if( num % 2 == 1 ) {
    // it's odd
}

Last edited on
Thanks to everyone, the code below works like you guys suggested but I still have problems finding the count for the odd numbers. I try the counter like "integralfx (874)" suggested but I don't think I'm doing it correct. Does anyone know what I'm doing wrong ? or how can I rewrite this program so that's more simple to read for a new person like myself to go line by line and see what's actually happening?

#include <iostream>
using namespace std;
int main()
{
int high=500;
int low=50;
int odd=0;
// Initialize first three Fibonacci Numbers
int f1 = 0, f2 = 1, f3 = 1;

// Count fibonacci numbers in given range
int result = 0;

while (f1 <= high)
{
if (f1 >= low)
result++;
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
if( result % 2 == 1 )
{
odd++;
}


cout << odd << " ";
cout << result << " ";
return 0;
}
Guys , it looks like I got the problem working in C++ code, and I would like to thank everyone for taking the time to share their knowledge. I also wanted to ask if anyone knows a good website to learn assembly language using the pep 8 virtual machine?

#include <iostream>
using namespace std;
int main()
{
int high;
int low;
int odd=0;
// Initialize first three Fibonacci Numbers
int f1 = 0, f2 = 1, f3 = 1;

// Count fibonacci numbers in given range
int result = 0;
cout << "Enter low and high limits:" << endl;
cin >> low;
cin >> high;

while (f1 <= high)
{
if (f1 >= low)
result++;
f1 = f2;
f2 = f3;
f3 = f1 + f2;

// take the numbers that are between low and high only and check to see if they are odd
if (f3 >= low && f3 <=high )
{
if( f3 % 2 == 1 )
odd++;
cout << f3 << " ";

}
}

cout << endl;
cout << odd << " of the " << result << " numbers printed were odd" << endl;
//cout << result << " ";
return 0;
}
Topic archived. No new replies allowed.