#include<stdlib.h>
#include<stdio.h>
long double fact(unsigned long int n)
/*The factorial of a positive integer by recursion*/
{
if (n==0)
return 1;
else
return n*fact(n-1);
}
int main()
{
long double sum, n;
int i, m;
printf("\t/*Code to find the approximate value of e */");
check:
printf("\n\n\tPlease Enter the value of n := ");
scanf("%lf", &n);
sum=0;
for (i=0; i<=n; i++)
sum +=1/(fact(i));
printf("\n\n\tThe appriximate value of e := %.15lg\n\n\t", sum);
printf("Let's do this again? 1/ YES Any key/ NO := ");
scanf("%d", &m);
if (m==1)
goto check;
else (1);
return 0;
}
This code worked perfectly well with Visual C++ 2010 but not with DEV C++. It kept on returning zero for the value of e. Can someone please explain why! Thanks!
use this scanf("%Lf", &n);
the Format specifier of long double is %Lf
,%le
,%lg
so use this
and also consider these point
Dev-C++ It uses GCC (MinGW). No ammount of Dev-C++ updates will fix this issue.
Dev-C++ has not been updated for 5 years; don't hold your breath.
The derivative wxDev-C++ is maintained.
If you want a compiler update, go to www.mingw.org, and hope that the latest version still works with the ancient Dev-C++. Or use a different IDE or compiler. I'd recommend VC++ 2010 Express Edition (Free, and a far better debugger), your code works as is in VC++ 2008, but that does not make it safe.
How did you determine the value was "0"? It is likely that it was in fact something like 0.000001, and your method of interrogation rounded it. You should view the value in the debugger rather than a print statement. Unfortunately Dev-C++'s debugger sucks.
The ISO C standard library defines functions only for double precision. If you want versions defined for float, you need to compile as C++ and include . C does not support function overloading, so it is impossible to do in C without differently named functions.