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 "stdafx.h"
#include <iostream>
usingnamespace 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;
}