In the previous article, we have discussed about Java Program to Print Series 3 6 9 12 15 18 …N
In this article we are going to see how to print the Sum of Series (1+2)/(1*2) + (1+2+3)/(1*2*3) + …… + (1+2+3+…+N)/(1*2*3*…*N) by using Java programming language.
Java Program to Print the Sum of Series (1+2)/(1*2) + (1+2+3)/(1*2*3) + …… + (1+2+3+…+N)/(1*2*3*…*N)
Let’s see different ways to find the series.
Method-1: Java Program to Print the Sum of Series (1+2)/(1*2) + (1+2+3)/(1*2*3) + …… + (1+2+3+…+N)/(1*2*3*…*N) By Using User Input Value
Approach:
- Create Scanner class object.
- Prompt the user to enter a number.
- Initialize the variable
sum = 0
. - Run a for loop
i=2 to n+1
(otherwise it won’t print the desired sum). - Inside the loop, initialize two variables
num
andden
both to 0. - Run a nested for loop
j=1 to i
and updatenum+=j
and den asden*=j
. - Then outside the inner loop update sum variable as
sum = sum + num/den
. - Print the result outside the loops.
Program:
import java.util.Scanner; public class Main { public static void main(String args[]) { // create scanner class object Scanner in = new Scanner(System.in); // take user input System.out.print("Enter n: "); int n = in.nextInt(); // initialize sum to 0 double sum = 0.0; // loop to find the sum of the series for (int i = 2; i <= (n+1); i++) { double num = 0.0, den = 1.0; // inner loop to calculate numerator // and denominator at ith positon for (int j = 1; j <= i; j++) { num += j; den *= j; } // updating sum variable sum = sum + (num / den); } // printing the result System.out.println("Sum=" + sum); } }
Output: Enter n: 5 Sum=3.0708333333333333
Method-2: Java Program to Print the Sum of Series (1+2)/(1*2) + (1+2+3)/(1*2*3) + …… + (1+2+3+…+N)/(1*2*3*…*N) By Using User-Defined Method
- In method 1, a lot of things were happening inside the for loop and it’s hard to figure out what actually is the program doing.
- Therefore, it is usually preferable to break down the programs into subtasks and define methods as required.
- In this case the program primarily has 3 subtasks
- Finding sum upto ith position
- Finding product upto ith position
- Dividing sum by product.
- And finally, the addition of the terms.
- The 3rd and the 4th subtasks are just one liners and hinder the readability of the code but task 1 and 2 are a bit lengthy and relatively complicated so we may move them into new methods and call these methods in the driver method (in this case the main method)
Approach:
- Create Scanner class object.
- Prompt the user to enter a number.
- Initialize the variable
sums = 0
. - Run a for loop
i=2 to n+1
. - Inside the loop, update
sums = sums + sum(i) / product(i)
; - Here the sum() and product are user defined methods which will return the sum and product (num and den in method 1 respectively).
- Print sums outside the loop.
- Now define a method product which takes an argument
i
of integer type and returns a double. - Inside the method initialize a variable prod as 1.0
- Run a for loop
j=1 to i
and update prod asprod *= j
. - Similarly, define a method sum which takes an argument
i
of integer type and returns a double. - Inside the method initialize a variable
s
as 0.0 - Run a for loop
j=1 to i
and updates
ass += j
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // create a Scanner object Scanner sc = new Scanner(System.in); // prompt the user to enter the number of terms System.out.print("Enter the number of terms: "); int n = sc.nextInt(); // initialize sum variable double sums = 0.0; // call the method to print the series for (int i = 2; i <= (n + 1); i++) { sums += sum(i) / product(i); } System.out.println("The sum of the series is: " + sums); } // method to find the product from 1 to i private static double product(int i) { double prod = 1; for (int j = 1; j <= i; j++) { prod *= j; } return prod; } // method to find the sum from 1 to i private static double sum(int i) { double s = 0.0; for (int j = 1; j <= i; j++) { s += j; } return s; } }
Output: Enter the number of terms: 5 The sum of the series is: 3.0708333333333333
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output
Related Java Programs: