In the previous article, we have discussed about Java Program to Find the Sum of Series 1 – x2/2! + x4/4! – x6/6! + …… xn/n
In this article we are going to see how to find sum of the series (x^2 / 1!) + (x^4 / 3!) + (x^6 / 5!) + … N by using Java programming language.
Java Program to Print the Series (x^2 / 1!) + (x^4 / 3!) + (x^6 / 5!) + … N
Let’s understand the series with an example.
Example:
Suppose the value of X = 2 N = 5 Then series is like: (2^2 / 1!) + (2^4 / 3!) + (2^6 / 5!) => 4 + 16/6 + 64/120 = 7.2
Let’s see different ways to find sum of the series (x^2 / 1!) + (x^4 / 3!) + (x^6 / 5!) + … N
Method-1: Java Program to Find the Sum of Series (x2 / 1!) + (x4 / 3!) + (x6 / 5!) + …… + N By Using For Loop
Approach:
- Declare an int variable say ‘x’ which holds the number of whose we will be calculating the sum of the series.
- Declare an int variable say ‘n’ which holds the last exponent value of the series.
- Create Scanner class object.
- Prompt the user to enter values for
a
andn
. - By using for loop find term and keep track on its 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 x "); int x = s.nextInt(); //Taking input of number of elements in the series System.out.println("Enter the value of last exponent 'n' "); int n = s.nextInt(); //for loop to print the series double sum = 0; for (int i = 2; i <= 2 * n; i += 2) { long b = 1; for (int j = 1; j <= i - 1; j++) { b *= j; } double t = Math.pow(x, i) / b; sum += t; } System.out.println("Sum = " + sum); } }
Output: Enter the value of x 2 Enter the value of last exponent 'n' 5 Sum = 7.253615520282186
Method-2: Java Program to Find the Sum of Series (x2 / 1!) + (x4 / 3!) + (x6 / 5!) + …… + N By Using While Loop
Approach:
- Declare an int variable say ‘x’ which holds the number of whose we will be calculating the sum of the series.
- Declare an int variable say ‘n’ which holds the last denominator value of the series
- Create Scanner class object.
- Prompt the user to enter values for
a
andn
. - By using while loop find term and keep track on its 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 x "); int x = s.nextInt(); //Taking input of number of elements in the series System.out.println("Enter the value of last exponent 'n' "); int n = s.nextInt(); //while loop to print the series double sum = 0; int i = 2; while(i <= 2 * n) { long b = 1; for (int j = 1;j <= i - 1;j++) { b *= j; } double t = Math.pow(x, i) / b; sum += t; i += 2; } System.out.println("Sum = " + sum); } }
Output: Enter the value of x 2 Enter the value of last exponent 'n' 5 Sum = 7.253615520282186
Method-3: Java Program to Find the Sum of Series (x2 / 1!) + (x4 / 3!) + (x6 / 5!) + …… + N By Using User Defined Method
Approach:
- The same logic as method 1 but this time we are moving the for inside a user-defined method
- Create Scanner class object.
- Prompt the user to enter a number for “x”
- Prompt the user to enter the last denominator value for “n”
- Call a method to execute the sum series
- By using for loop find term and keep track on its 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 x "); int x = s.nextInt(); //Taking input of number of elements in the series System.out.println("Enter the value of last exponent 'n' "); int n = s.nextInt(); // calling m1 method to print the series m1(x,n); } public static void m1(int x, int n) { //for loop to print the series double sum = 0; for (int i = 2; i <= 2 * n; i += 2) { long b = 1; for (int j = 1; j <= i - 1; j++) { b *= j; } double t = Math.pow(x, i) / b; sum += t; } System.out.println("Sum = " + sum); } }
Output: Enter the value of x 5 Enter the value of last exponent 'n' 10 Sum = 371.0160038182103
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: