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
When a thread blocks on i/o, what state does it enter?
what are getters and setters in Java with examples?
Explain about RMI Architecture?
What modifiers may be used with an inner class that is a member of an outer class?
how do you Handle Front End Application data against DB with example?
Difference between DurableSubscription and non- DurableSubscription?
What is prototype?
Explain the advantages and disadvantages of detached objects.
What is the difference between long.class and long.type?
What are externizable interface?
What happens when a thread cannot acquire a lock on an object?
Why are some of the class and element names counter-intuitive?
What is Stream Tokenizer?
What is scalable, portability in the view of J2EE?
Can I have an action without a form?