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 is codebase in applet?

2 Answers  


what are the software's needed to develop advanced java 3 tier application project

1 Answers  


Will it be called overriding if I do not change the parameters or return type, instead throw a different exception in the method signature.

4 Answers   HeadStrong,


How can I scroll through list of pages like the search results in google?

0 Answers  


Should synchronization primitives be used on bean methods?

0 Answers  






Why a component architecture for the java platform?

0 Answers  


whats is mean by tiles in struts

2 Answers   SolutionNET,


On a computer that having single CPU, how multithreading concept can be achieved?

1 Answers   Zensar,


Is a class a subclass of itself?

0 Answers  


What is RMI and what are the services in RMI?

0 Answers  


How would you reatach detached objects to a session when the same object has already been loaded into the session?

0 Answers  


How has the sandbox changed with Java 2?

2 Answers  


Categories