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..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Scanner;
  2.  
  3. public class JavaLoopExcercise
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner console = new Scanner(System.in);
  8.  
  9. int number;
  10.  
  11. double sum = 0;
  12.  
  13. System.out.print("Enter number of terms of series : ");
  14. number = console.nextInt();
  15.  
  16. for(int i = 1; i <= number; i++)
  17. {
  18. sum += 1.0/i;
  19. }
  20.  
  21. System.out.println("sum: " + sum);
  22. }
  23. }

Result

java techstudy.org

     





Related Topics




Popular Posts