class Harmonic

{

public static void main(String…s)

{

int n,i;

float sum=0;

n=Integer.parseInt(s[0]);

for(i=1;i<=n;i++)

{

sum=sum+(float)1/i;

}

System.out.println(“nSum=”+sum);

}

}

Skip to content
# Java program to find sum of harmonic series 1 + 1/2 + 1/3 + 1/4 + 1/5 +……+ 1/n

### You May Also Like:

###
19 thoughts on “Java program to find sum of harmonic series 1 + 1/2 + 1/3 + 1/4 + 1/5 +……+ 1/n”

### Leave a Comment

class Harmonic

{

public static void main(String…s)

{

int n,i;

float sum=0;

n=Integer.parseInt(s[0]);

for(i=1;i<=n;i++)

{

sum=sum+(float)1/i;

}

System.out.println(“nSum=”+sum);

}

}

deepaclass Harmonic

{

public static void main(String…s)

{

int n,i;

float sum=0;

n=Integer.parseInt(s[0]);

for(i=1;i<=n;i++)

{

sum=sum+(float)1/i;

}

System.out.println(“nSum=”+sum);

}

}

Jatin SinghPlz tell the code for harmonic series

S = 1-1/2 + 1/3 – 1/4…… -1/10

Ayush tripathiclass Harmonic

{

public static void main(String…s)

{

int n,i;

float sum=0;

n=Integer.parseInt(s[0]);

double sign=1;

for(i=1;i<=n;i++)

{

sum=sum+((float)1/i*sign);

sign*=-1;

}

System.out.println(“nSum=”+sum);

}

}

Napsterimport java .util.Scanner;;

public class Main

{

public static void main(String[] args) {

Scanner s = new Scanner (System.in);

int n = s.nextInt();

double sum= 0;

for (float i = 1; i<=n;i++){

sum+=(1/i)*Math.pow(-1,i+1);

}

System.out.println(sum);

}

}

sriram krishnaI want Harmonic series program for the below expression.

1/(n+1)+1/(n+2)+1/(n+3)+………1/(n+n);

please,help me for this.

sriram krishnastill there no update for my request.please send the reply for the request

Arghadeep Palint s=0;

for(i=1;i<=n;I++)

{

s=s+(1/(n+I));

}

Sopln(s);

Samreen fatmaHow to find the sum of series

Question:

1*1+ 2*2+3*3*3+4*4+5*5+6*6*6+……

saswatthanx a lot bro…

Deepanshu Vashistwhy is (float) used in following statement?

sum=sum+(float)1/i;

userBecause if you divide 1 by i which is an integer your ans will be an integer. E.g. if i=2, 1/i will give you 0 and not 0.5, that’s why you typecast either numerator or denominator to float, so that the division 1/i is also float

soorajmahatoimport java.util.Scanner;

class sum_of_hp

{

public static void main(String args[])

{

double num,i,sum=0;;

Scanner sc=new Scanner(System.in);

System.out.print(“upto how many terms you want to sum = “);

num=sc.nextInt();

for(i=1;i<=num;i++)

{

sum=sum+(1/i);

}

System.out.println("sum of the series upto "+num+" terms is " +sum);

}

}

instead of using typecasting why didnot you take double type it is easier than type casting

soorajimport java.util.Scanner;

class sumofhp

{

public static void main(String args[])

{

double num,i;

double sum=0;

Scanner sc=new Scanner(System.in);

System.out.print(“upto how many terms you want to sum = “);

num=sc.nextInt();

for(i=1;i<=num;i++)

{

sum=sum+(1/i);

}

System.out.println("sum of the series upto "+num+" terms is " +sum);

}

}

soorajinstead of taking two different data type int and float can i use double type?

Estarding SyiemiongHow to find Series of

1+2/3+3/5+4/7….n.

Fatimaimport java.util.Scanner;

public class sumofseries {

public static void main(String[] args)

{

double sum = 0;

int n;

System.out.println(“1!/1+2!/2+3!/3+4!/4+5!/5”);

Scanner s = new Scanner(System.in);

System.out.print(“Enter the no. of terms in series:”);

n = s.nextInt();

sumofseries obj = new sumofseries();

for(int i = 1; i 0)

{

mul = mul * x;

x–;

}

return mul;

}

}

Kishan SoniHow to find 9th n for the below formula

H(n,r) = 1/1^r + 1/2^r + … + 1/n^r

sahilhow to find sum of series 1 +(1+ 1/2^2) +(1+1/ 2^2 +1/3^3 )+ …..+(1+1/ 2^2 +1/3^3 +……+ 1/n^n)

rupayan biswasWrite a javascript program to create a function which takes an argument n and calculate the sum of the following series: (1+½+⅓+¼+……….+1/n). Use for loop to execute code. Call the function and print output. Your output code should return the output variable.