Q:

Write a java program to calculate the sum of following series where n is input by user

0

Write a java program to calculate the sum of following series where n is input by user

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
 
public class JavaLoopExcercise
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
 
        int number;  
 
        double sum = 0;
 
        System.out.print("Enter number of terms of series : ");
        number = console.nextInt();
 
        for(int i = 1; i <= number; i++)
    {
            sum += 1.0/i;
    }
 
        System.out.println("sum: " + sum);
    }  
}

Result:

Enter number of terms of series : 10

sum: 2.9289682539682538

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< Write a Java program to print Fibonacci series of ...