Fibonacci Small Problem Help

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;

}







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
#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
    double num;
    double a = 0;
	double b = 1;
	double sum;
    
	cout << "How many Fibonacci numbers do you want to display? ";
    cin >> num;
	cin.ignore();
    
    if (num >= 0 && num <= 100)
    {
		
    
		for (int i = 0; i < num; i++) //don't use a double for your loop counter
		{
        
			if (i == num - 1)
			{
				cout << a << '.';
			}
			else
			{
				cout << a << ", ";
				sum = a + b;
				a = b;
				b = sum;
			}
		}/*end of for loop*/

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


	//system("pause"); //don't use system pause
	cin.ignore();
	return 0;
}


For me, Use <space> per number. Then print the period after the process
Topic archived. No new replies allowed.