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

Introduction

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

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

Leave a Comment