Problem: 3.11

 

Question 3.11: Write a program to calculate the sum of following series where n is the input given by the user.

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


Solution:




package com.company;
import java.util.Scanner;
public class Lab03Problem11{
    public static void main(String[] args) {
        Scanner Obj = new Scanner(System.in);
        System.out.print("Enter the size: ");
        int n = Obj.nextInt();
        float sum=0;
        for(int i=1;i<=n;i++){
            sum =  sum +(float) 1/i;
        }
        System.out.println("factorial result is: " + sum);
    }
}

Previous Post Next Post