In the previous article we have discussed about Java Program to Print the Series 24 99 224 399 624 899 …N
In this article we are going to see how to print sum of the series 1 + (1/2) + (1/3) + …… + (1/N) by using Java programming language.
Java Program to Find the Sum of Series 1 + (1/2) + (1/3) + … + (1/N)
On observing the pattern carefully, we can see each term is in a/b
format.
Numerator = 1 (Fixed)
Denominator = It starts from 1 and in each term incremented with 1
Like
1st term=1/1
2nd term=1/2
3rd term=1/3 like this.
And the series= 1/1+1/2+1/3 like this.
And our task is to first form the series and then find the sum of the series.
Let’s see different ways to print sum of the series 1 + (1/2) + (1/3) + …… + (1/N).
Example:
Suppose value of n = 3 1 + (1/2) + (1/3) = 1 + 0.5 + 0.34 = 1.84
Method-1: Java Program to Find the Sum of Series 1 + (1/2) + (1/3) + … + (1/N) By Using For Loop
Approach:
- Declare an integer variable say ‘
n
’ which holds the value of Nth term. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Declare an long variable say ‘
sum
‘ and initialize it to 0. - Use a for loop
from i=1 to i<=100
(incremented by 1) - Inside the for loop, find each term and add it to
sum
. - 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 value of 'n' "); int n = s.nextInt(); double sum = 0; //for loop to print the series for (int i = 1; i <= n; i++) { sum += (1.0 / i); } System.out.println("Sum of the series is " + sum); } }
Output: Enter the value of 'n' 3 Sum of the series is 1.8333333333333333
Method-2: Java Program to Find the Sum of Series 1 + (1/2) + (1/3) + … + (1/N) By Using While Loop
Approach:
- Declare an integer variable say ‘
n
’ which holds the value of Nth term. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Declare an long variable say ‘
sum
‘ and initialize it to 0. - Use a for loop
from i=1 to i<=100
(incremented by 1) - Inside the for loop, find each term and add it to
sum
. - 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 value of 'n' "); int n = s.nextInt(); double sum = 0; int i=1; //while loop to print the series while (i <= n) { System.out.println(1.0/i); sum += (1.0 / i); i++; } System.out.println("Sum of the series is " + sum); } }
Output: Enter the value of 'n' 3 Sum of the series is 1.8333333333333333
Method-3: Java Program to Find the Sum of Series 1 + (1/2) + (1/3) + … + (1/N) By Using While Loop
Approach:
- Declare an integer variable say ‘
n
’ which holds the value of Nth term. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Then call a user defined method say
printSeries()
by passingn
as parameter. - Inside method declare an long variable say ‘
sum
‘ and initialize it to 0. - Use a for loop
from i=1 to i<=100
(incremented by 1) - Inside the for loop, find each term and add it to
sum
. - 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 value of 'n' "); int n = s.nextInt(); //calling printSeries() method to print the series printSeries(n); } public static void printSeries(int n) { double sum = 0; //for loop to print the series for (int i = 1; i <= n; i++) { sum += (1.0 / i); } System.out.println("Sum of the series is " + sum); } }
Output: Enter the value of 'n' 4 Sum of the series is 2.083333333333333
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Related Java Programs: