Fibonacci problem

I have this assignment due today but i don't quite understand this question.
Write a program that outputs Fibonacci numbers. This part I understand I have this it lets you input a number and it'll create a fubonacci sequence of that length.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
typedef unsigned long long ull;
int main() {
int N;
cout << "Enter the N : ";
cin >> N;
ull f0 = 0, f1 = 1;
ull f = f1;
cout << "The Sequence of Fibonacci Numbers : " << endl;
cout << f0 << " ";
cout << f1 << " ";

for (int i = 1; i < N; i++) {
    cout << f << " ";
    f0 = f1;
    f1 = f;
    f = f0 + f1;
}
cout << endl;
return 0;
}



WHAT I DON'T UNDERSTAND is this part of the assignment.. any of you guys able to make sense of this?
"Using a while loop and two or three integer variables, have your program output a new Fibonacci number to the screen each time the user enters a key (use getchar()!)."
Simple, just use the function getch() :
1
2
3
4
5
6
for (int i = 1; i < N; i++) {
    cout << f << " ";
    f0 = f1;
    f1 = f;
    f = f0 + f1; getch();
}
could you elaborate?
Nothing to elaborate. The function just pauses the program until the user presses a key, and so on.
The instructions say it must be in a while loop, so modify accordingly.
So instead of for (int i = 1; i < N; i++), use while() :

1
2
int i = 1;
while (i++ < N)
closed account (48T7M4Gy)
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
#include <iostream>
typedef unsigned long long ull;

int main()
{
    int i = 0;
    
    int N;
    std::cout << "Enter the N: ";
    std::cin >> N;
    
    ull f0 = 0, f1 = 1;
    ull f = f1;
    
    std::cout << "The Sequence of Fibonacci Numbers:\n";
    std::cout << f1 << ' ';

    while ( i++ < N - 1 )
    {
        std::cout << f << ' ';
        f0 = f1;
        f1 = f;
        f = f0 + f1;
    }
    
    return 0;
}
Enter the N: 10
The Sequence of Fibonacci Numbers: 
1 1 2 3 5 8 13 21 34 55  
Last edited on
@kemort
> "Using a while loop and two or three integer variables, have your program output a new Fibonacci number to the screen each time the user enters a key (use getchar()!)"
closed account (48T7M4Gy)
Don't tell me, @closed account & numerous numbers and extraneous letters, tell him!
Last edited on
Topic archived. No new replies allowed.