Fibonacci Series Help (Period Problem)

Hey everyone,

I was asked to write a code that has the user input a number and then the computer calculates it for the Fibonacci series. The output should be separated by commas and a period should follow the last number. Ex. 1,2,3,4,5. <---period

I can't seem to get the period at the end. I have the commas and everything else. Thanks for the help! =) Here is my code:


#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
    double num;
    
    cout << "How many Fibonacci numbers do you want to display?";
    cin >> num;
    
    if (num >= 0 && num <= 100)
    {
    double a = 0;
    double b = 1;
    double sum;
    
    for (double i = 0; i < num; i++)
    {
        cout << a << ", ";
        sum = a + b;
        a = b;
        b =sum;
    }

    }
    
    else

    cout << "Error! Please enter a number in the range of 0-100.";


system("pause");

return 0;

}







check this out

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
#include <iostream>
using namespace std;


int main()
{
    double num;

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

    if (num >= 0 && num <= 100)
    {
    double a = 0;
    double b = 1;
    double sum;

    for (double i = 0; i < num; i++)
    {
        cout << a << ", ";
        sum = a + b;
        a = b;
        b =sum;
    }

    cout << a; // <<<<<<< here

    }

    else

    cout << "Error! Please enter a number in the range of 0-100.";


return 0;

}
Last edited on
Oh, I see now! Thanks so much!
#include <iostream>
using namespace std;


int main()
{
double num;
double i;

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

if (num >= 0 && num <= 100)
{
double a = 0;
double b = 1;
double sum;

for (i = 1; i <= num; i++)
{
cout << a ;
sum = a + b;
a = b;
b =sum;
if (i<num){
cout << ", ";
}
}

if (i=num)
{
cout << ".";
}
}

else

cout << "Error! Please enter a number in the range of 0-100.";


cin.get();

return 0;

}
Topic archived. No new replies allowed.