In the previous article we have discussed about Java Program to Print Series 0 7 26 63 …N
In this article we are going to see how to find sum of the series 1/2 + 1/3 + 1/4 + 1/5 + … + 1/N by using Java programming language.
Java Program to Display Sum of Series 1/2 + 1/3 + 1/4 + 1/5 + … + 1/N
On observing the pattern carefully, we can see
Numerator is 1 (fixed)
In Denominator, the number starts from 2 and the next number is 1 added with the previous denominator number.
‘n’ is the no of nth term in the series.
Example:
Suppose value of n = 3 Then the sum of series is 1/2 + 1/3 + 1/4 = 0.5 + 0.34 + 0.25 = 1.09
Let’s see different ways to find sum of the series 1/2 + 1/3 + 1/4 + 1/5 + … + 1/N.
Method-1: Java Program to Display Sum of Series 1/2 + 1/3 + 1/4 + 1/5 + … + 1/N By Using For Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the value of number of terms of the series. - Prompt the user to enter a number as value of
n
. - Declare an double variable say ‘
sum
’ and assign the value to 0. - Use a for loop from
i=2 to i<=n+1
and incremented by 1 - Inside loop we will find the value of
1 / i
, and then we will add that value insum
for each iteration. - Print the result.
Program:
import java.util.*; public class Main { public static void main(String [] args) { //creating object of Scanner class Scanner s = new Scanner(System.in); //Taking input of number of elements in the series System.out.println("Enter the number of term "); int n = s.nextInt(); //for loop to print the series double sum = 0; for (int i = 2; i <= n+1; i++) { double result =(double) 1 / i; sum += result; } System.out.println("Sum of the series is " +sum); } }
Output: Enter the number of term 3 Sum of the series is 1.0833333333333333
Method-2: Java Program to Display Sum of Series 1/2 + 1/3 + 1/4 + 1/5 + … + 1/N By Using While Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the value of number of terms of the series. - Prompt the user to enter a number as value of
n
. - Declare an double variable say ‘
sum
’ and assign the value to 0. - Declare and initialize an integer variable
i=2
- Continue a while loop from
i=2 to i<=n+1
and incremented by 1. - Inside loop we will find the value of
1 / i
, and then we will add that value insum
for each iteration. - Print the result.
Program:
import java.util.*; public class Main { public static void main(String args[]) { //creating object of Scanner class Scanner s = new Scanner(System.in); //Taking input of number of elements in the series System.out.println("Enter the number of term "); int n = s.nextInt(); //while loop to print the series double sum = 0; int i = 2; while (i <= n+1) { double result =(double) 1 / i; sum += result; i ++; } System.out.println("sum of the series is " + sum); } }
Output: Enter the number of term 3 sum of the series is 1.0833333333333333
Method-3: Java Program to Display Sum of Series 1/2 + 1/3 + 1/4 + 1/5 + … + 1/N By Using User Defined Method
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n
’ which holds the value of number of terms of the series. - Prompt the user to enter a number as value of
n
. - Then call a user defined method
printSeries()
and passn
as parameter. - Inside method declare an double variable say ‘
sum
’ and assign the value to 0. - Use a for loop from
i=2 to i<=n+1
and incremented by 1 - Inside loop we will find the value of
1 / i
, and then we will add that value insum
for each iteration. - Print the result.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // creating object of scanner class Scanner s = new Scanner(System.in); //Taking input of number of elements in the series System.out.println("Enter the number of term "); int n = s.nextInt(); // calling m1 method to print the series printSeries(n); } //user defined method printSeries() to print the series public static void printSeries(int n) { //for loop to print the series double sum = 0; for (int i = 2; i <= n+1; i++) { double result =(double) 1 / i; sum += result; } System.out.println("Sum of the series is " +sum); } }
Output: Enter the number of term 7 Sum of the series is 1.7178571428571427
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Programs: