In the previous article, we have discussed about Java Program to Find the Sum of Series (a*2) + (a*3) + (a*4) + …… + N
In this article we are going to see how to find sum of the series 2-4+6-8+…-/+N by using Java programming language
Java Program to Find the Sum of Series 2-4+6-8+…-/+N
On observing the pattern carefully, we can see first number is positive and second number is negative.
While 1st number starts with 2, 2nd number is 2 added with previous number.
Example:
X = 2
N = 20
2 – 4 + 6 – 8 + …… – 20 = -10
Let’s see different ways to find sum of the series 2-4+6-8+…-/+N.
Method-1: Java Program to Find the Sum of Series 2-4+6-8+…-/+N By Using For Loop
Approach:
- Declare an integer variable say ‘
n
’ which holds the last term of the series. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Declare an integer variable say ‘
sum
‘and initialize it with 0. - Use a for loop
from i =1 to i<=n/2
- Inside for loop we will first check the index of
i
, ifi
is even then we will subtracti*2
fromsum
Else we will addi*2
in 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 last number 'n' "); int n = s.nextInt(); //for loop to print the series int sum = 0; for (int i = 1; i <= n/2; i++) { if (i % 2 == 0) sum -= i * 2; else sum += i * 2; } System.out.println("Sum of the series is " + sum); } }
Output: Enter the value of last number 'n' 20 Sum of the series is -10
Method-2: Java Program to Find the Sum of Series 2-4+6-8+…-/+N By Using While Loop
Approach:
- Declare an integer variable say ‘
n
’ which holds the last term of the series. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Declare an integer variable say ‘
sum
‘and initialize it with 0. - Declare an integer variable
i
and initialize it with 1. - Continue a while loop
till i<=n/2
- Inside while loop we will first check the index of
i
, ifi
is even then we will subtracti*2
fromsum
Else we will addi*2
in 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 last number 'n' "); int n = s.nextInt(); // while loop to print the series int sum = 0; int i = 1; while(i <= n/2) { if (i % 2 == 0) sum -= i * 2; else sum += i * 2; i++; } System.out.println("Sum of the series is " + sum); } }
Output: Enter the value of last number 'n' 40 Sum of the series is -20
Method-3: Java Program to Find the Sum of Series 2-4+6-8+…-/+N By Using User Defined Method
Approach:
- Declare an integer variable say ‘
n
’ which holds the last term of the series. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Then call a user defined method say
m1()
and passn
as parameter. - Inside method declare an integer variable say ‘
sum
‘and initialize it with 0. - Use a for loop
from i =1 to i<=n/2
- Inside for loop we will first check the index of
i
, ifi
is even then we will subtracti*2
fromsum
Else we will addi*2
in 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 last exponent 'n' "); int n = s.nextInt(); // calling m1 method to print the series m1(n); } public static void m1(int n) { //for loop to print the series int sum = 0; for (int i = 1; i <= n/2; i++) { if (i % 2 == 0) sum -= i * 2; else sum += i * 2; } System.out.println("Sum of the series is " + sum); } }
Output: Enter the value of last exponent 'n' 10 Sum of the series is 6
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Related Java Programs: