Computer Science, asked by mukesh9838, 11 months ago

write a program to determine and print the sum of the following harmonic series for a given value of n. 1 + 1/2 + 1/3 +....+ 1 /n.​

Answers

Answered by Anonymous
4

import java.util.*;

public class Sum

{

public static void main(String args[])

{

//since we don't know the value of 'n' so, we have to use scanner class to declare the value of 'n'.

Scanner in = new Scanner(System.in);

int a, n;

double s=0;

//the variable 's' is taking for sum and we should accept the value either in float or double operand because, the series is in fraction and the sum will must decimal point.

System.out.println("Enter the value of n");

n = in.nextInt();

//as the program is a terminating one so we should apply loop here

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

{

s=s+1/a;

System.out.println("The sum is"+s);

}

}

}

//the part in bold letter is for the program and others for explanation.

Answered by Sumit0041
2
Import java.util.*;
Public class sum

{

Public static void
Main (string args [])

{

//since we don’t know the value
Of ‘n’ so,we have to use scanner
Class to declare the value of ‘n’
Scanner in = new
Scanner(system.in);
Int a,n;
Double s=0;
//the variable ‘s’ is taking for sum
And we should accept the value
either in float or double operand
Because, the series is in fraction
and the sum will must decimal point

System.out.println(“Enter the value of n”);

n = in.nextlnt();

//as the program is a terminating one so we should apply loop here

{

S=s+1/a;

System.out.print(“The sum is”+s);

}

}

}

//the part in bold letter is for the program and other for explanation
Similar questions