Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Explain about thread synchronization inside a monitor?

2606


Name the eight primitive java types.

1202


What is the RMI and Socket?

1097


What restrictions are placed on the values of each case of a switch statement?

1061


For which statements does it make sense to use a label?

1134


What is the difference between session and entity beans?

1175


What is the relation between the infobus and rmi?

1066


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

1157


What is local interface. How values will be passed?

1093


Why use POJO when I can use hashmap

2716


How would you create a button with rounded edges?

1244


What is the form of storage space in java?

2299


What is the purpose of the finally clause of a try-catch-finally statement?

1076


Define the remote object implementation?

2569


Which textcomponent method is used to set a textcomponent to the read-only state?

1183