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
To what value is a variable of the string type automatically initialized?
How would you reatach detached objects to a session when the same object has already been loaded into the session?
Name three subclasses of the component class?
What is the relationship between local interfaces and container-managed relationships?
Difference between new operator and class.forname().newinstance()?
Where we can write Rmi registry in the code, without having to write it at the command prompt?
Describe activation process?
Which javutil classes and interfaces support event handling?
Do I need to import javlang package any time? Why ?
What are various types of class loaders used by jvm?
Explain about thread synchronization inside a monitor?
How are the elements of a cardlayout organized?
What is table mutation and how do you avoid it?
What is mdb and what is the special feature of that?
If I wanted to use a solarisui for just a jtabbedpane, and the metal ui for everything else, how would I do that?