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;
}
#include <iostream>
usingnamespace 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;
}