Write a java program to find out the sum of harmonic series :
1 + ½ + 1/3 + ……… up to nth term , for any value of n.

Answers were Sorted based on User's Feedback



Write a java program to find out the sum of harmonic series : 1 + ½ + 1/3 + ……… up to ..

Answer / sourav dey

import java.io.*;
class Hermonic{
public static void main(String args[]){
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
double result = 0.0;
int num=0;
try{
System.out.println("Enter a term which you want to sum:");
num = Integer.parseInt(in.readLine());
}
catch(Exception e){}
while(num > 0){
result = result +( (double) 1 / num);
num--;
}
System.out.println("Output of Harmonic Series is "+result);
}
}

Is This Answer Correct ?    12 Yes 0 No

Write a java program to find out the sum of harmonic series : 1 + ½ + 1/3 + ……… up to ..

Answer / 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

More Advanced Java Interview Questions

What state does a thread enter when it terminates its processing?

0 Answers  


I wantr to know, How many number of users logged in to website? can any one answer

7 Answers   Cap Gemini,


how the action can be map from jsp page to bean class in mvc1

1 Answers   Photon,


What is J2EE?

2 Answers  


Explain bind(), rebind(), unbind() and lookup() methods?

1 Answers  






What are the benefits of detached objects?

0 Answers  


What is DTD?

3 Answers   Wipro,


What are the advanatages of RMI ?

1 Answers   HGS,


How do you maintain a stateful session object across the session

2 Answers   HCL,


What happens when we invoke a thread?s interrupt method while it is in sleeping or waiting condition?

1 Answers  


Name three component subclasses that support painting?

0 Answers  


What is the highest-level event class of the event-delegation model?

0 Answers  


Categories