Hey guys i was wondering if anyone can help me with this

im new at java but my teacher really has it that we are at this level already, i get the basic knowlege but it would be awesome if anyone would help. So far i got the factorial but i dont know how to implement it into the program.

-------------------------------------------------

One of the more amazing facts from calculus is that the following sum gets closer and closer to the value ex the more terms you add in:

ex = 1 + x + x2/2! + x3/3! + x4/4! + x5/5! + x6/6! + . . . .

Remember that n! means n factorial, n*(n-1)*(n-2)* ... *1. For example, if x is 2 then

e2 = 1 + 2 + 22/2! + 23/3! + 24/4! + 25/5! . . . .

e2 = 1 + 2 + 4/2 + 8/6 + 16/24 + 32/120 + . . . .

e2 = 1 + 2 + 2 + 1.3333 + 0.6666 + 0.2666 + . . . .

e2 ~ 7.266

More exactly, e2 = 7.38907...

Write a program that asks the user to enter x, then calculates ex using a loop to add up successive terms until the current term is less than 1.0E-12. Then write out the value Math.exp(x) to see how your value compares.

To do this program sensibly, the loop will add in a term each iteration.

sum = sum + term;

Look carefully at the first equation for ex. Notice that each term in the series is:

x N/N!

for some N. This is the same as:

x(N-1)/(N-1)! * x/N

This is the previous term times x/N. So each iteration of the loop merely has to multiply the previous term by x/N and add it to the accumulating sum.

Don't let the math scare you away! This is actually a fairly easy program, and is typical of a type of calculation that computers are often used for.

Enter x:

2

n:1 term: 2.0 sum: 3.0

n:2 term: 2.0 sum: 5.0

n:3 term: 1.3333333333333333 sum: 6.333333333333333

n:4 term: 0.6666666666666666 sum: 7.0

n:5 term: 0.26666666666666666 sum: 7.266666666666667

n:6 term: 0.08888888888888889 sum: 7.355555555555555

n:7 term: 0.025396825396825397 sum: 7.3809523809523805

n:8 term: 0.006349206349206349 sum: 7.387301587301587

n:9 term: 0.0014109347442680777 sum: 7.3887125220458545

n:10 term: 2.8218694885361555E-4 sum: 7.388994708994708

n:11 term: 5.130671797338464E-5 sum: 7.389046015712681

n:12 term: 8.551119662230774E-6 sum: 7.3890545668323435

n:13 term: 1.3155568711124268E-6 sum: 7.389055882389215

n:14 term: 1.8793669587320383E-7 sum: 7.3890560703259105

n:15 term: 2.5058226116427178E-8 sum: 7.389056095384136

n:16 term: 3.1322782645533972E-9 sum: 7.389056098516415

n:17 term: 3.6850332524157613E-10 sum: 7.389056098884918

n:18 term: 4.094481391573068E-11 sum: 7.389056098925863

n:19 term: 4.309980412182177E-12 sum: 7.3890560989301735

n:20 term: 4.309980412182177E-13 sum: 7.389056098930604

my e^x: 7.389056098930604

real e^x: 7.38905609893065

----------------------------------------------------------------------------

# need help with Java factorial

Page 1 of 1## 3 Replies - 3610 Views - Last Post: 19 October 2010 - 11:29 AM

##
**Replies To:** need help with Java factorial

### #2

## Re: need help with Java factorial

Posted 19 October 2010 - 10:29 AM

We will not give you code. Post your attempt(s) at solving you problem. We will help you debug it and suggest improvements.

Use the code tags:

Use the code tags:

### #3 Guest_Ghosted*

## Re: need help with Java factorial

Posted 19 October 2010 - 10:57 AM

n8wxs, on 19 October 2010 - 09:29 AM, said:

We will not give you code. Post your attempt(s) at solving you problem. We will help you debug it and suggest improvements.

Use the code tags:

Use the code tags:

Thanks for a reply, and i dint want the code just a mental breakdown of what i need to add =]

so far i have

import java.util.Scanner; class ConrolledLoop{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); double inData; System.out.println("Enter n!: "); inData = scan.nextDouble(); double N = inData; //User input double factorial= 1; System.out.println(+N+"! is " +factorial); for (double i= 1; i<=N; i++){ factorial=factorial*i; } System.out.println(factorial); } }

That is what i have so far, but that is just to firgure out the factorial. I need a way so that When the user inputs X it would power X to itself and factor X! so it would look like this e^x = 1 + x + x^2/2!

then after the first x^2/2! it adds one to the power and one to the factorial and now it would look like

e^x = 1 + x + x^2/2!+x^3/3!.. i hope that helps im sorry if im being a such pain, im kinda new to this.

### #4 Guest_Ghosted*

## Re: need help with Java factorial

Posted 19 October 2010 - 11:29 AM

Ghosted, on 19 October 2010 - 09:57 AM, said:

n8wxs, on 19 October 2010 - 09:29 AM, said:

We will not give you code. Post your attempt(s) at solving you problem. We will help you debug it and suggest improvements.

Use the code tags:

Use the code tags:

Thanks for a reply, and i dint want the code just a mental breakdown of what i need to add =]

so far i have

import java.util.Scanner; class ConrolledLoop{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); double inData; System.out.println("Enter n!: "); inData = scan.nextDouble(); double N = inData; //User input double factorial= 1; System.out.println(+N+"! is " +factorial); for (double i= 1; i<=N; i++){ factorial=factorial*i; } System.out.println(factorial); } }

That is what i have so far, but that is just to firgure out the factorial. I need a way so that When the user inputs X it would power X to itself and factor X! so it would look like this e^x = 1 + x + x^2/2!

then after the first x^2/2! it adds one to the power and one to the factorial and now it would look like

e^x = 1 + x + x^2/2!+x^3/3!.. i hope that helps im sorry if im being a such pain, im kinda new to this.

This is my new code it give me 3 when i put 2 in

import java.util.Scanner; class ConrolledLoop{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); double inData; System.out.println("Enter the term"); inData = scan.nextDouble(); double F = inData; double P = inData; double N = inData; double factorial= 1; double e = 0; for (double i= 1; i<=P; i++){ } for (double i= 1; i<=F; i++){ factorial=factorial*i; } e= Math.pow((N), P)/F; double n = e+1; System.out.println(n); } }

now i just gotta make it loop so that it give me 5 then 6.333333333333333 etc. any help

Page 1 of 1