Why, when I use double i
the output is (an approximation to) the value of e?
#include <iostream>
using namespace std;
int main ()
{
double s=0;
double i=1;
for (int m=1;m<5;m++)
{
i=m*i;
s=s+1/i;
}
cout<<s+1;
return 0;
}
But when I use int i
, the output is 2:
#include <iostream>
using namespace std;
int main ()
{
double s=0;
int i=1;
for (int m=1;m<5;m++)
{
i=m*i;
s=s+1/i;
}
cout<<s+1;
return 0;
}
The variable that stores the value of e is s
, which is double
, so I was expecting that the datatype of i
doesn't matter.