I am trying to find out the solution for the series 1 + 1/2! + 1/3! + ......... + 1/n! (n! = Factorial)! But when I store 1/factorial of 5 or suppose 6 in a variable the value of the variable is 0.0 but I should be 0.16666666666. So there lie the problem. I dry ran
my program and could not find any problem in my logic but in line 31 the problem is rising! The data type of the variable is double and it should store decimal values but! Please help!
Ranajoy Saha wrote:I dry ran my program and could not find any problem in my logic but in line 31 the problem is rising! The data type of the variable is double and it should store decimal values but! Please help!
Yes, but the problem is actually occurring at line 30.
Q: What is the result of 1/factorial? (Tip: look at the types carefully).
Ranajoy Saha wrote:I just changed the data type of factorial variable and the work was done! But I need an explanation. when the data type of factorial variable is int and when I change line 30 to series_calc =(double) (1/factorial); then why isn't the same answer coming as previous one. (The answer is 1.0) I am confused on this part! Dont be angry on me if my question seems silly 'cause I am in school and I have just stated using Java language!
No probs. The answer is basically as fred said, so the result of 1/factorial is an int. Adding the cast as you have done simply the changes the type of that result; it doesn't change its value.
BTW, you didn't need to change the type of factorial. What do you think the result of 1.0/factorial is?