Write a program to determine the sum of the following harmonic series for a given value of n: 1+1/2+1/3……………………..+1/n the value of n should be given interactively through the keyboard.
PROGRAM:
import java.util.Scanner; class P4 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); double sum=0,n,i; System.out.println("Please Enter the value of N:="); n=sc.nextDouble(); for(i=1;i<=n;i++) { sum=sum+(1/i); if(i==1) System.out.print("1 + "); else if(i==n) System.out.print("1/ "+i); else System.out.print("1/ "+i+" + "); } System.out.println(); System.out.println("THE SUM OF THIS SERIES IS:="+sum); } }
OUTPUT:
(Visited 5,860 times, 1 visits today)
Written by: