Java program to generate Harmonic Series 1 + 1/2 + 1/3 + 1/4 + 1/5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* Write a program to generate Harmonic Series.
   Example :
   Input - 5
   Output - 1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28 (Approximately) */
class HarmonicSeries{
	public static void main(String args[]){
		int num = Integer.parseInt(args[0]);
		double result = 0.0;
		while(num > 0){
			result = result + (double) 1 / num;
			num--;
		}
		System.out.println("Output of Harmonic Series is "+result);
	}
}
surajk

18 thoughts on “Java program to generate Harmonic Series 1 + 1/2 + 1/3 + 1/4 + 1/5

  1. import java.io.*;

    class h2
    {
    public static void main(String args[])
    {
    int n=Integer.parseInt(args[0]);
    double r=0.0;
    while(n>=0)
    {
    r=r+(double) 1 / n;
    n–;
    }
    System.out.println(“output of harmonic series is ” + r);
    }
    }

    i tried that above program but i’l get a Array index out of bounds exception error.. i dont know how to rectify that error…kindly rectify it …pls

  2. public class display
    {
    public static void main(String ar[])
    {
    int a=1,i;
    double s=0.0;
    for(i=1;i<=5;i++)
    {
    s=s+a/i;
    }
    System.out.println("series="+s);
    }
    }

  3. public class HermonicSeries {
    public static void main(String args[])
    {
    int i;
    double result=0.0;
    for(i=1; i<=5; i++)
    {
    result=result + (double) 1/i;
    }
    System.out.println("1+1/2+1/3+1/4+1/5 = "+result);
    }

    }

  4. why we are using double????? in result= result
    = 1/i;
    why its not like result = result + 1/i;

  5. compile: javac h2.java
    run: java h2 8
    see here, we are using command line arguments. so in command prompt after compiling we have run the program with java space , as i given a example above. if you are not interested in command line arguments then use “Scanner” class from “util” package.

  6. this may be a good solution to yours type of output.
    ————————————————————————————-
    class HarmonicSeries
    {
    public static void main(String args[])
    {
    int num = Integer.parseInt(args[0]);
    double result = 0.0;
    System.out.println(“Output of Harmonic Series is:”);
    for (int i=1;i 0)
    {
    result = result + (double) 1 / num;
    num–;
    }

    System.out.println(” = ” +result);
    }
    }

  7. try this…
    its good in look and better output.
    ——————————————————-
    class HarmonicSeries
    {
    public static void main(String args[])
    {
    int num = Integer.parseInt(args[0]);
    double result = 0.0;
    System.out.println(“Output of Harmonic Series is:”);
    for (int i=1;i 0)
    {
    result = result + (double) 1 / num;
    num–;
    }

    System.out.println(” = ” +result);
    }
    }

  8. class HarmonicSeries
    {
    public static void main(String args[])
    {
    int num = Integer.parseInt(args[0]);
    double result = 0.0;
    System.out.println(“Output of Harmonic Series is:”);
    for (int i=1;i 0)
    {
    result = result + (double) 1 / num;
    num–;
    }

    System.out.println(” = ” +result);
    }
    }

  9. since int/int gives int.
    use this : result = result + 1.0/i; i.e double/int gives double, and result is a double variable.

  10. Genuinely great content. I merely stumbled upon your blog site and
    sought tto note that I’ve surely preferred browsing your weblog content.
    Yet again shortly No matter the reason I’ll be subscribing in your
    nourish and I am hoping you prepare once more

  11. It will just produce the final result by multiplying…….how to generate whole series like 1+1/2+1/3+1/4+1/5………
    Any Idea Please? 🙂

  12. import java.util.*;
    class series{
    public static void main(String args[]){
    int i,n,s=0;
    Scanner sc=new Scanner(System.in);
    System.out.println(“Enter The Range = “);
    n=sc.nextInt();
    for(i=1;i<=n;i++)
    s=i/(i+1);
    System.out.println("The Sum = "+s);
    }
    }

  13. import java.util.*;
    class series{
    public static void main(String args[]){
    int i,n;
    double s=0;
    Scanner sc=new Scanner(System.in);
    System.out.println(“Enter The Range = “);
    n=sc.nextInt();
    for(i=1;i<=n;i++)
    s=1/i;
    System.out.println("The Sum = "+s);
    }
    }

  14. How to print this series :- 7.7+77.77+777.777+7777.7777+77777.77777+777777.777777+7777777.7777777 pls do reply soon!!

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in