Java BigDecimal calculate e

Question

We would like to calculate e using BigDecimal.

e = 1 + 1/1! + 1/2! + 1/3! + 1/4! + ... + 1/i!

Write a program that displays the e value for i = 100, 200, ..., and 1000.

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
  public static void main(String[] args) {
    for (int i = 100; i <= 1000; i += 100) {
      /*from   w ww  .ja v  a  2  s  . c o m*/
      //your code here
      
      System.out.println("i = " + i + ": " + e);
    }
  }
}



import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
  public static void main(String[] args) {
    for (int i = 100; i <= 1000; i += 100) {
      BigDecimal e = new BigDecimal("1");
      BigDecimal item = new BigDecimal("1");
      for (int n = 2; n <= i; n++) {
        e = e.add(item);
        item = item.divide(new BigDecimal(String.valueOf(n)), 25,
          RoundingMode.HALF_UP);
      }
      System.out.println("i = " + i + ": " + e);
    }
  }
}



PreviousNext

Related