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
what is handle?
What is message driven beam?
Why doesn’t the focus feature on the tag work in every circumstance?
What are the different methods of identifying an object?
Which javutil classes and interfaces support event handling?
Is “abc” a primitive value?
whats is statement and procedure
What is the relationship between local interfaces and container-managed relationships?
Is there a guarantee of uniqueness for entity beans?
Can I run seam with jdk 1.4 and earlier?
What do you need to set-up a cluster with jboss?
what is the use of State Factories?
What is JTS?
Brief description about local interfaces?
What is the difference between RMI registry and OS Agent?