In the previous article we have discussed about Java Program to print Series 6 14 36 98 … N
In this article we are going to see how to print sum of the series X^1 + X^2 + X^3 + … + X^N by using Java programming language
Java Program to Display Sum of Series X^1 + X^2 + X^3 + ……… X^N
On observing the pattern carefully, we can see
X is fixed and can be anything.
Sum series = X^1 + X^2 + X^3 + … + X^n,
where 1,2,3,..,n are the powers of X in each term respectively.
Example:
Suppose the value of X = 2 and n= 3 Then Sum of series = 2^1 + 2^2 + 2^3 = 2+4+8 = 14
Let’s see different ways to print sum of the series X^1 + X^2 + X^3 + … + X^N
Method-1: Java Program to Display Sum of Series X^1 + X^2 + X^3 + ……… X^N By Using For Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
x
’, which holds a fixed value. - Declare an integer variable say ‘
n
’, which holds the Nth term power of the series. - Prompt the user to enter 2 number as values of ‘
x
’, ‘n
’ respectively . - Let declare another integer variable say ‘
result
’ and initialize it to 0. - Use a for loop from
i=1 to i<=n
, where the loop is incremented by 1 - Inside the loop we will find the value of
x^i
and then add that value with theresult
. - 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 N "); int n = s.nextInt(); int result = 0; //for loop to print the series for (int i = 1; i <= n; i++) { result +=Math.pow(x,i); } System.out.println("Sum of the series is " + result); } }
Output: Enter the value of x 2 Enter the value of N 3 Sum of the series is 14
Method-2: Java Program to Display Sum of Series X^1 + X^2 + X^3 + ……… X^N By Using While Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
x
’, which holds a fixed value. - Declare an integer variable say ‘
n
’, which holds the Nth term power of the series. - Prompt the user to enter 2 number as values of ‘
x
’, ‘n
’ respectively . - Let declare another integer variable say ‘
result
’ and initialize it to 0. - Declare and initialize an integer variable
i=1
- Continue a while loop till
i<=n
, where the loop is incremented by 1 - Inside the loop we will find the value of
x^i
and then add that value with theresult
. - 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 N "); int n = s.nextInt(); int result=0; System.out.print(result); int i=1; while(i<=n) { result +=Math.pow(x,i); i++; } System.out.println("Sum of the series is " + result); } }
Output: Enter the value of x 2 Enter the value of N 9 0Sum of the series is 1022
Method-3: Java Program to Display Sum of Series X^1 + X^2 + X^3 + ……… X^N By Using User Defined Method
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
x
’, which holds a fixed value. - Declare an integer variable say ‘
n
’, which holds the Nth term power of the series. - Prompt the user to enter 2 number as values of ‘
x
’, ‘n
’ respectively . - Let declare another integer variable say ‘
result
’ and initialize it to 0. - Use a for loop from
i=1 to i<=n
, where the loop is incremented by 1 - Inside the loop we will find the value of
x^i
and then add that value with theresult
. - 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 N "); int n = s.nextInt(); // calling printSeries method to print the series printSeries(x,n); } //printSeries metthod to print the series public static void printSeries(int x, int n) { int result = 0; System.out.print(result); //for loop to print the series for (int i = 1; i <=n; i++) { result +=Math.pow(x,i); } System.out.println("Sum of the series is " + result); } }
Output: Enter the value of x 5 Enter the value of N 10 0Sum of the series is 12207030
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Applications On:
- X+X^2+X^3+…+X^N Program In Java
- 1+X+X^2+X^3+…+X^N Program In Java
- Display The Sum Of The Series 1 + X + X^2/2! + X^3/3! + … In Java
- Summation Of X^N
- Sum Of Series In Java
- 1+X+X^2+X^3+…+X^N Program In C
- Sum Of Series Program In Java
Related Java Programs: