Week_2.3

mail@pastecode.io avatar
unknown
plain_text
2 months ago
608 B
2
Indexable
Never
// Importing required packages
import java.util.Scanner;

// Driver Code
public class lab3{
    public static void main(String args[]){
		// Creating a Scanner class object to take user input
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the value of n : ");
        int n = scanner.nextInt();

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

        }

        System.out.printf("Sum of the following harmonic series for a given value of n is %.2f\n",sum);

        // Closing the object to free up the memory
		scanner.close();
    }
}
Leave a Comment