3:50 pm - Monday February 19, 7596

Java Example Program for harmonic series

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

/**
*Author : Candid.java
*Description : Program for Harmonic series
*/

import java.io.*;

public class HarmonicSeries {
public static void main(String args[]) {
int num = Integer.parseInt(args[0]); // convert the input string to
// integer
double result = 0.0;
while (num > 0) {
result = result + (double) 1 / num; // formula for harmonic series
num--;
}
System.out.println("Output of Harmonic Series is " + result);
}
}

Output :

E:\karthik>javac HarmonicSeries.java

E:\Candidjava>java HarmonicSeries 5
Output of Harmonic Series is 2.283333333333333

E:\Candidjava>java HarmonicSeries 3
Output of Harmonic Series is 1.8333333333333333

Filed in: Z-A All Java Codes

Comments are closed.