Java program – Harmonic numbers

public class Harmonic { 
    public static void main(String[] args) { 

        // command-line argument
        int N = Integer.parseInt(args[0]);

        // compute 1/1 + 1/2 + 1/3 + ... + 1/N
        double sum = 0.0;
        for (int i = 1; i <= N; i++) {
            sum += 1.0 / i;
        }

        // print out Nth harmonic number
        System.out.println(sum);
    }

}


/*************************************************************************
 *  Execution:    java Harmonic N
 *  
 *  Prints the Nth harmonic number: 1/1 + 1/2 + ... + 1/N.
 * 
 *  % java Harmonic 10
 *  2.9289682539682538
 *
 *  % java Harmonic 10000
 *  9.787606036044348
 *
 *************************************************************************/

About Nideesh C

Me, freelance system administrator having the qualification of Diploma in Electronics & Tele-communication + MCSE + CCNA + CST + 5 years of experience in IT field.

Leave a Reply

Your email address will not be published. Required fields are marked *