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


Please Help Members By Posting Answers For Below Questions

When a thread blocks on i/o, what state does it enter?

792


what are getters and setters in Java with examples?

1486


Explain about RMI Architecture?

816


What modifiers may be used with an inner class that is a member of an outer class?

801


how do you Handle Front End Application data against DB with example?

1716


Difference between DurableSubscription and non- DurableSubscription?

1927


What is prototype?

810


Explain the advantages and disadvantages of detached objects.

780


What is the difference between long.class and long.type?

761


What are externizable interface?

785


What happens when a thread cannot acquire a lock on an object?

744


Why are some of the class and element names counter-intuitive?

805


What is Stream Tokenizer?

1944


What is scalable, portability in the view of J2EE?

2166


Can I have an action without a form?

806