This is a Java Program to Generate Harmonic Series.
We take a number as an input and using loops we generate the Harmonic series and get the desired output.
Here is the source code of the Java Program to Generate Harmonic Series. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
class Harmonic
{
public static void main(String... a)
{
System.out.print("Enter any number : ");
Scanner s = new Scanner(System.in);
int num = s.nextInt();
System.out.print("The Harmonic Series is : ");
double result = 0.0;
while(num > 0)
{
result = result + (double) 1 / num;
num--;
System.out.print(result +" ");
}
System.out.println("");
System.out.println("Output of Harmonic Series is "+result);
}
}
Output:
$ javac Harmonic.java $ java Harmonic Enter any number : 5 The Harmonic Series is : 0.2 0.45 0.7833333333333333 1.2833333333333332 2.283333333333333 Output of Harmonic Series is 2.283333333333333
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Programming Books
- Practice Information Technology MCQs
- Check Java Books
- Apply for Java Internship
- Practice Programming MCQs