0
int main()
{
    int n, t1 = 0, t2 = 1,t3 = 2, nextTerm = 0;

    cout << "Enter the number of terms: "; cin >> n;

    cout << "Fibonacci number sequence up to " << n << ":" << endl;

    for (int i = 1; i <= n+1; i++)
    {
        if (i == 1)
        {
            cout << " " << t1;
            continue;
        }
        if (i == 2)
        {
            cout << " " << t2 << " ";
            continue;
        }
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
        cout << nextTerm << "";
        cout << endl;
    }
    cout << "Number of times iterative function is called: " << n * 3 << endl;
    return 0;
} 

Output:

Enter the number of terms: 5
Fibonacci number sequence up to 5: 
0 1 1
2  
3
5
Number of times iterative function is called: 15

I want the output to be:

Enter the number of terms: 5
Fibonacci number sequence up to 5:
0 1 1 2 3 5
Number of times iterative function is called: 15
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
ih8coding
  • 27
  • 8

4 Answers4

1

Just put the line cout << endl; outside of the for loop :

int main()
{
    int n, t1 = 0, t2 = 1,t3 = 2, nextTerm = 0;

cout << "Enter the number of terms: "; cin >> n;

cout << "Fibonacci number sequence up to " << n << ":" << endl;

for (int i = 1; i <= n+1; i++)
{
    if (i == 1)
    {
        cout << " " << t1;
        continue;
    }
    if (i == 2)
    {
        cout << " " << t2 << " ";
        continue;
    }
    nextTerm = t1 + t2;
    t1 = t2;
    t2 = nextTerm;
    cout << nextTerm << "";
}
cout << endl;
cout << "Number of times iterative function is called: " << n * 3 << endl;
return 0;
CsuGouv
  • 249
  • 1
  • 9
1

The only problem is "endl" after the "nextTerm". It will print the new number on next line. Just put the "endl" outside the for loop and it will show the desired output.

Saboor
  • 352
  • 1
  • 10
0
int main()
 {
   int n, t1 = 0, t2 = 1,t3 = 2, nextTerm = 0;

   cout << "Enter the number of terms: "; cin >> n;

   cout << "Fibonacci number sequence up to " << n << ":" << endl;

    for (int i = 1; i <= n+1; i++)
    {
        if (i == 1)
        {
            cout << " " << t1;
            continue;
        }
        if (i == 2)
        {
            cout << " " << t2 << " ";
            continue;
        }
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
        cout << nextTerm << " ";
    }
    cout << endl;
    cout << "Number of times iterative function is called: " << n * 3 << endl;
    return 0;
}

Notice the cout << endl; is out of the for block now to write it only once to the output, instead of being written after every iteration.

Also add a write a space character in the last line of the for loop.

Petok Lorand
  • 955
  • 6
  • 15
0

Use endl or \n

#include <iostream>
using namespace std;

int main() {
    int n, a = 0, b = 1, c;
    cout << "Enter the number of terms: ";
    cin >> n;
    cout << "\nFibonacci number sequence up to " << n << ":"<<"\n";
    if(n >= 0) cout << a << " ";
    if(n >= 1) cout << b << " ";
    for(int i = 2; i <= n; i++){
        c = a + b;
        cout<<c<<" ";
        a = b;
        b = c;
    }
    cout << "\nNumber of times iterative function is called: " << n * 3; 
    return 0;
}
Ishpreet
  • 5,230
  • 2
  • 19
  • 35