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

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

  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


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.