Write a java program to find out the sum of harmonic series :
1 + ½ + 1/3 + ……… up to nth term , for any value of n.
Answer Posted / s
package com.adder;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class GeneralJava {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new GeneralJava().process
(10));
}
private double process(int n) {
BigDecimal retVal = BigDecimal.ZERO;
for (int i=1; i<= n; i++) {
BigDecimal adder =
BigDecimal.ONE.divide(BigDecimal.valueOf(i),10,
RoundingMode.HALF_UP);
retVal = retVal.add(adder);
}
return retVal.doubleValue();
}
}
| Is This Answer Correct ? | 4 Yes | 4 No |
Post New Answer View All Answers
Explain about thread synchronization inside a monitor?
Name the eight primitive java types.
What is the RMI and Socket?
What restrictions are placed on the values of each case of a switch statement?
For which statements does it make sense to use a label?
What is the difference between session and entity beans?
What is the relation between the infobus and rmi?
When a thread blocks on i/o, what state does it enter?
What is local interface. How values will be passed?
Why use POJO when I can use hashmap
How would you create a button with rounded edges?
What is the form of storage space in java?
What is the purpose of the finally clause of a try-catch-finally statement?
Define the remote object implementation?
Which textcomponent method is used to set a textcomponent to the read-only state?