Question

In: Computer Science

In Java!! Problem 2 – Compute the sum of the series (19%) The natural logarithm of...

In Java!!

Problem 2 – Compute the sum of the series (19%)

The natural logarithm of 2, ln(2), is an irrational number, and can be calculated by using the following series:

1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 - 1/8 + ... 1/n

The result is an approximation of ln(2). The result is more accurate when the number n goes larger.
Compute the natural logarithm of 2, by adding up to n terms in the series, where n is a positive integer and input by user.

Requirements:

  1. Use either for or while loop to display the number pattern.

Solutions

Expert Solution

CODE

==============

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int n;
       System.out.println("Enter the number of terms in the series: ");
       n = sc.nextInt();
      
       double sum = 0;
      
       for (int i = 1; i<=n; i++) {
           double term = 1.0/i;
           if (i % 2 == 0) {
               sum -= term;
           } else {
               sum += term;
           }
          
           System.out.println("Term " + i + " = " + term);
       }
      
       System.out.println("ln(2) = " + sum);
   }
}


Related Solutions

A. For a series of 1500 tosses, what is the natural logarithm of the total number...
A. For a series of 1500 tosses, what is the natural logarithm of the total number of microstates associated with 50% heads and 50% tails? (Note: Stirling’s approximation will be useful in performing these calculations). B. How much less probable is the outcome that the coin will land 40% heads and 60% tails? (Note: Stirling’s approximation will be useful in performing these calculations).
2. Based on the data below: a) Plot the percentage of insects versus the natural logarithm...
2. Based on the data below: a) Plot the percentage of insects versus the natural logarithm of the dose. b) Convert the data to probit variable, and plot the probit versus the natural logarithm of the dose. c) Determine the probit constants and the LC50. Please show all calculations. Dose of rotenone (mg/l) Number of insects Number affected (deaths) 9.1 60 47 7.0 73 51 6.4 49 32 5.3 35 20 4.4 54 18
2. Based on the data below: a) Plot the percentage of insects versus the natural logarithm...
2. Based on the data below: a) Plot the percentage of insects versus the natural logarithm of the dose. b) Convert the data to probit variable, and plot the probit versus the natural logarithm of the dose. c) Determine the probit constants and the LC50. Please show all calculations. Dose of rotenone (mg/l) Number of insects Number affected (deaths) 9.1 60 47 7.0 73 51 6.4 49 32 5.3 35 20 4.4 54 18
It is desired to compute the sum of the first 10 terms of the series 14k3 = 20k2 + 5k, k = 1, 2, 3, …
It is desired to compute the sum of the first 10 terms of the series14k3 = 20k2 + 5k, k = 1, 2, 3, … a. Develop a pseudocode description of the required program.b. Write and run the program described in part a.
write a java code Write a generic program to compute the sum of 30 terms of...
write a java code Write a generic program to compute the sum of 30 terms of the following series. (181 - 5)/31 + (186 + 9)/34 - (191 - 13)/37 + (196 + 17)/40 + . . . . . 1 5 11 Note: the 3rd, 6th, 9th term etc, has a negative sign in front of parenthesis. User Input: None Expected output: Term Ratio Sum 1 5.677 5.677 2 5.735 11.413 3 -4.811 6.602
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
Provide the Java code to compute the sum, average, maximum number and minimum number if I...
Provide the Java code to compute the sum, average, maximum number and minimum number if I have a string sentence of double numbers. Assume that the length of the string sentence is not known. It can be of any length. To split a string based on the comma character use the following. String sentence = "A,B,C,D,E"; String[] stringsArray = receivedSentence.split(","); Then stringsArray is an array of five elements such that: stringsArray[0] = 'A' stringsArray[1] = 'B' stringsArray[2] = 'C' stringsArray[3]...
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number...
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number of input integers or float numbers. In other words, calls like this should all work: flexible_sum(x1, x2) # sum of two integer or float numbers or strings x1 and x2 flexible_sum(x1, x2, x3) # sum of 3 input parameters and can also handle a single input parameter, returning it unchanged and with the same type, i.e.:   flexible_sum(1) returns 1 and can accept an arbitrary...
Determine the sum of the series ∑n=1∞ ( 6n)/(n+2) if possible. (If the series diverges, enter...
Determine the sum of the series ∑n=1∞ ( 6n)/(n+2) if possible. (If the series diverges, enter 'infinity', '-infinity' or 'dne' as appropriate.)
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3!...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3! /3+4! /4+5! /5 using the function1, • to convert decimal number to binary number using the function2, • to check whether a number is a prime number or not using the function3, • to check whether two given strings are an anagram using the function4. important must do in (Multi-Filing) of c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT