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 Bootstrapping in RMI?

1 Answers  


A user of a web application sees a jsessionid argument in the URL whenever a resource is accessed. What does this mean? a. The form must have the field jsessionid b. URL rewriting is used as the session method c. Cookies are used for managing sessions

0 Answers  


What is synchronization and why is it important?

0 Answers  


What is meant by Superconductivity?

4 Answers  


Is the ternary operator written x : y ? Z or x ? Y : z ?

0 Answers  






How are the elements of a cardlayout organized?

0 Answers  


What is meant by method chaining?

0 Answers  


Explain about RMI Architecture?

0 Answers  


Can I use javascript to submit a form?

0 Answers  


what is heepStored?

1 Answers  


Why are my checkboxes not being set from on to off?

0 Answers  


What is RPC?

2 Answers  


Categories