# Help with basic program for euler number

 Doug Nicholas · October 10, 2015 Hi everyone, this is my very first topic and yes, I´m asking for help.I´m meant to write a code to get an approximate value of Euler´s number or Napier´s (how I´m used to call it) "e".We know that e = 2.71.. and some more annoying numbers,  and that this little guy can be expressed as this :but, I only manage to get the sum of the first 6 numbers or 8 (because I don´t know how to make it infinite). Anyway, here is the code I tried to run, but the only problem is that when I compile my program, it keeps displaying "2" just that:Can you guys tell me what I´m doing wrong? And if possible... how can I have an infinite sum of those numbers?Thanks!``// approximation of euler´s number with infinte sums of the reciprocals of factorials#include long factorialResult (int number) { // defining my function of factoriallong result = 1 ;if (number != 0){  do {result *= number ; number -- ;} while (number != 0) ;} else {result = 1 ;}return result ;} using namespace std;int main (){int number = 0;int amount = 0;float sum = 0 ;float factor ; do {factor = (1/factorialResult(number)) ;sum += factor;number ++ ;amount ++ ; }while (amount < 13) ;cout << " Euler´s number is: "<< sum; // this keeps displaying "1" when we know that e = 2.71.... return sum ;}``

## Replies

 Doug Nicholas · October 15, 2015 Thanks Lim Soo Zhin ! Your solution worked! About the amount variable, yes, you are right I should not use it but it was a way to organize my ideas and get the code rather more understandable for me. Thank you very much anyways!. Jon Z. · October 13, 2015 use double as data type. Jon Z. · October 13, 2015 use double as data type. Lim Soo Zhin · October 13, 2015 I happen to figure out your problem.The problem is the return data type of your function is "long". It does not contain any decimals. So when 1/factorialResult (int number)" , you get a long data type. You won't get any decimals.The solution:"long factorialResult (int number) { // defining my function of factorial"Instead of using long, use float. That will solve your problem.Also, in your main: int number = 0;int amount = 0;float sum = 0 ;float factor ; do {factor = (1/factorialResult(number)) ;sum += factor;number ++ ;amount ++ ; }while (amount < 13) ;You don't need to use the variable amount. Delete the variable amount and change the last "amount" in the parentheses of while to "number" and the program will work just fine. c student · October 10, 2015 when you try to store a floating point number in the process of a calculation, one of the numbers in the calculation must also be a floating point number
• 1

## C++

129,743 followers