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
Can constructors be synchronized in java?
What is the difference between the session.update() method and the session.lock() method?
Is the ternary operator written x : y ? Z or x ? Y : z ?
Which characters may be used as the second character of an identifier, but not as the first character of an identifier?
which type of objects reference will be given to client?
when A client sent a request to the server to open facebook page and close the browser after this request .at that time the same user do login by using a different browser then that session id will exist or not for the same client??
How would you detect a keypress in a jcombobox?
What are the different methods of identifying an object?
What is a clone?
What modifiers may be used with an inner class that is a member of an outer class?
Difference between hashmap and hashtable?
Explain about local interfaces.
What is a modular application? What does module-relative mean?
Explain the difference between object state and behavior?
What you mean by COM and DCOM?