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
Why is actionform a base class rather than an interface?
Do I have to use jsps with my application?
What is the relationship between the canvas class and the graphics class?
What is TL and its use?
What do you need to set-up a cluster with jboss?
What is the infobus?
Are enterprise beans allowed to use thread.sleep()?
What is jboss?
Can I map more than one table in a cmp?
Are there books about seam?
What are callback interfaces?
Why do threads block on i/o?
Explain about RMI Architecture?
what are the advantages of JTA over JTS?
What restrictions are placed on the location of a package statement within a source code file?