In the previous article, we have discussed about Java Program to Print Series 5 10 15 20 25 30 …N
In this article we are going to see how to print the Sum of Series 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + …… N by using Java programming language.
Java Program to Print the Sum of Series 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + …… N
In this series it can be seen that numbers at each position i, the term is calculated as ((i-1)x + i)/i!
For example:
For x = 2 at 3rd position the term is ((3-1)2 + 3)/3!=1.166666667
Here, for each we have to find the factorial of the number and add it to the sum.
Let’s see different ways to print the series.
Method-1: Java Program to Print the Sum of Series 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + …… N By Using User Input Value
Approach:
- Create Scanner class object.
- Prompt the user to enter the value of
xandn. - Initialize the sum variable to 1 (as the first term in the series is always 1).
- Run a for loop from
i=2ton. - Inside the loop, initialize variable
f=1, which will hold the factorial. - Use another nested loop from
j=1toito find the factorial. - Inside the nested loop, update
fasf*= j. - Outside the inner loop and inside the outer loop, initialize a variable as
t = ((i - 1) * x + i) / (double)f, which will hold the current term. - Then update sum as
sum = sum + t. - Print the sum outside the loops.
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// create scanner class object
Scanner sc = new Scanner(System.in);
// prompt user to enter x
System.out.print("Enter x: ");
int x = sc.nextInt();
// prompt user to enter n
System.out.print("Enter n: ");
int n = sc.nextInt();
double sum = 1;
// loop to calculate sum
for (int i = 2; i <= n; i++)
{
// finding factorial of n
double f = 1;
for (int j = 1; j <= i; j++)
{
f *= j;
}
// calculating the current term in the series
double t = ((i - 1) * x + i) / f;
// adding the current term to the sum
sum += t;
}
// print the result
System.out.println("Sum of the series is = " + sum);
}
}
Output: Enter x: 2 Enter n: 3 Sum of the series is = 4.166666666666667
Method-2: Java Program to Print the Sum of Series 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + …… N By Using User Defined Method
Approach:
Use the same approach as method 1 but move the nested loop to calculate the factorial inside a user-defined method.
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// create scanner class object
Scanner sc = new Scanner(System.in);
// prompt user to enter x
System.out.print("Enter x: ");
int x = sc.nextInt();
// prompt user to enter n
System.out.print("Enter n: ");
int n = sc.nextInt();
double sum = 1;
// loop to calculate sum
for (int i = 2; i <= n; i++)
{
// finding factorial of n
double f = fact(i);
// calculating the current term in the series
double t = ((i - 1) * x + i) / f;
// adding the current term to the sum
sum += t;
}
// print the result
System.out.println("Sum of the series is = " + sum);
}
// method to calculate factorial
private static double fact(int i)
{
double f = 1;
for (int j = 1; j <= i; j++)
{
f *= j;
}
return f;
}
}
Output: Enter x: 2 Enter n: 3 Sum of the series is = 4.166666666666667
Method-3: Java Program to Print the Sum of Series 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + …… N By Calculating Factorial Value Using Recursion
Approach:
Use the same approach as method 2 but the user-defined method will calculate the factorial value recursively.
- Define a method called fact which returns a double value take ‘
n’ as an argument of int data type. - Inside the recursive method, check if
(n == 1), return 1 (base condition). - Else return
n*fact(n-1)
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// create scanner class object
Scanner sc = new Scanner(System.in);
// prompt user to enter x
System.out.print("Enter x: ");
int x = sc.nextInt();
// prompt user to enter n
System.out.print("Enter n: ");
int n = sc.nextInt();
double sum = 1;
// loop to calculate sum
for (int i = 2; i <= n; i++)
{
// finding factorial of n
double f = fact(i);
// calculating the current term in the series
double t = ((i - 1) * x + i) / f;
// adding the current term to the sum
sum += t;
}
// print the result
System.out.println("Sum of the series is = " + sum);
}
// method to calculate factorial recursively
private static double fact(double n)
{
// base condition
if (n == 1)
return 1;
return n * fact(n-1);
}
}
Output: Enter x: 2 Enter n: 3 Sum of the series is = 4.166666666666667
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Programs: