java help

Discussion in 'OT Technology' started by airbball23, Oct 15, 2009.

1. airbball23Rent this Space only \$5/mnth

Joined:
Jan 13, 2007
Messages:
1,489
0
Hey guys.

I'm trying to do an assignment for class and i'm trying to figure this out:

"Write an application that estimates the value of math constant e by using the following formula:

e = 1 + (1/1!)+ (2/2!) + (3/3!)..."

This is the code i have and it's free of errors but it's not working, I'm not sure why not.

Code:
```package factorialb;
import java.util.Scanner;
/**
*
* @author airbball23
*/
public class Factorialb {

public static void main(String[] args) {
Scanner input = new Scanner (System.in);

double e =1;

e = input.nextInt();

for (int i= 1; i>=15; i++) {
int factorial = 0;

factorial = factorial * i;
e = e + 1 / (double) factorial;

System.out.println(e);
}

}
}
```
any suggestions?

2. Limp_BrisketNew Member

Joined:
Jan 2, 2006
Messages:
48,376
0
Location:
Utah
just at a quick glance, shouldn't it be "i / (double) factorial" and not "1 / (double) factorial"

3. airbball23Rent this Space only \$5/mnth

Joined:
Jan 13, 2007
Messages:
1,489
0
i tried it. no go.

actually the professor gave us the formula above.

4. airbball23Rent this Space only \$5/mnth

Joined:
Jan 13, 2007
Messages:
1,489
0
i keep getting this as the result:

Infinity
Infinity
Infinity
Infinity
Infinity
Infinity
Infinity
Infinity
Infinity
Infinity
Infinity
Infinity
Infinity
Infinity
Infinity

5. Limp_BrisketNew Member

Joined:
Jan 2, 2006
Messages:
48,376
0
Location:
Utah
you for loop conditional is wrong too, it should be i <= 15 not i >= 15.

6. StevesVR4Get Arrested

Joined:
Jul 1, 2003
Messages:
7,328
0
You are calculating your factorial wrong.
Code:
```int factorial  = 0;
factorial = factorial * i;
```
The above code will never give you the correct answer.

7. Limp_BrisketNew Member

Joined:
Jan 2, 2006
Messages:
48,376
0
Location:
Utah
hah. that's why he's getting Infinity, because he's dividing by zero every time.

i'm not a java guy, but doesn't it throw exceptions or something when stuff like that happens?

8. StevesVR4Get Arrested

Joined:
Jul 1, 2003
Messages:
7,328
0
I can't say that I have tried to divide by zero intentionally in Java to see the exact behaviour, but since we are dealing with doubles I can see why it wouldn't throw an exception. If the division dealt with integers, an exception would be thrown, but doubles have some special values that can be used in these situations. A double has NaN (not a number), positive infinity, and negative infinity that could be used. So in this case, when the double value is divided by zero, the positive infinity key value is used instead of throwing an exception.

9. airbball23Rent this Space only \$5/mnth

Joined:
Jan 13, 2007
Messages:
1,489
0
thanks guys. i'll try to post up the results once my professor posts it. i dont think anyone in the class got it.

10. DouggieJOT Supporter

Joined:
Nov 1, 2007
Messages:
8,127
0
Location:
New York, NY USA
This is easy:

Code:
```public class Factorialb {

public static void main(String[] args) {

double e = 1;
double denominator;

for (int i = 1; i <= 15; i++) {
denominator = factorial(i);
e = e + 1 / (double) denominator;
}
System.out.println(e + "\n");
}

private static int factorial (int x) {
if (x == 0)
return 1;
else
return (x * factorial (x - 1));
}
}
```
produces:

2.718281830084572