Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,664 members, 7,801,919 topics. Date: Friday, 19 April 2024 at 05:54 AM

Simple Java Problem - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Simple Java Problem (1800 Views)

Help A Java Newbie With This Simple Java Program / Please Solve This Java Problem. / Pls, Help Me In This Java Problem (2) (3) (4)

(1) (Reply) (Go Down)

Simple Java Problem by eazyd(m): 11:30am On Nov 20, 2012
Estimate and print the mathematical constant e (equal to 2.71828). You can estimate its value using the formula:
e = 1 + 1/1! + 1/2! + 1/3! +1/4! + ....+ 1/k! + ...
Quit looping when the term 1/k! becomes less than 1E-04.
Re: Simple Java Problem by eazyd(m): 11:33am On Nov 20, 2012
Specially for @Javanian - love the way you code! smiley
Re: Simple Java Problem by eazyd(m): 11:42am On Nov 20, 2012
solution-

public class ConstantValue
{
public static void main (String [] args)
{
// declare data;
double e;
double k;
double denom;
double term;

// initialize the data;
e = 1;
k = 0;
denom = 1;

do {

k++;
denom*= k;

term = 1/denom;

e += term;
System.out.println("The value of constant e = "+e);
}

while (term >= 1E-04);


}
}
Re: Simple Java Problem by Nobody: 12:45pm On Nov 20, 2012
.
Re: Simple Java Problem by Javanian: 1:15pm On Nov 20, 2012

public class ConstantValue {
public static void main(String[] args) {
double term = 2, e = 0;
final double difference = 0.0000000001; //or 1E-04

for (int n = 0; term > difference; n++) {
term = 1.0 / find_denominator(n);
e += term;
System.out.println("The Value of Constant e" + e);
}
}

public static double find_denominator(int n) {

if (n == 0 || n == 1)
return 1.0;

return n * find_denominator(n - 1);
}
}

2 Likes

Re: Simple Java Problem by Javanian: 1:23pm On Nov 20, 2012
eazyd: Specially for @Javanian - love the way you code! smiley
Am Flattered grin
Re: Simple Java Problem by eazyd(m): 1:46pm On Nov 20, 2012
Javanian:
Am Flattered grin

As usual, you delivered promptly!
Re: Simple Java Problem by xcitedjay(m): 8:45pm On Nov 24, 2012
Just thought I should add a solution using a while loop

public class ConstantValue{
public static void main(String[] args){
int k = 1;
double term = k, e = 0;
while(term > 1E-04){
e += term;
term = 1/compute(k++);
}
System.out.println("Constant e = " + e);
}
static double compute(int k){
if(k==1)
return k;
return k * compute(k-1);
}
}
Re: Simple Java Problem by Javanian: 3:25am On Jul 25, 2014
bump

(1) (Reply)

How To Register On Oyerr.com. / What Laptop Is Best For Learning Programming? / 5 Free Java Ebooks For Beginners On Clintonnzedimma's Request

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 9
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.