In the previous article, we have discussed about Java Program to Find the Sum of Series (1*2) + (2*3) + (3*4) + …… + N
In this article we are going to see how to find sum of the series (a*2) + (a*3) + …… + (a*N) by using Java programming language.
Java Program to Find the Sum of Series (a*2) + (a*3) + (a*4) + …… + (a*N)
On observing the pattern carefully, we can see terms are in the form of (a*b)
where ‘b
‘ value starts from 2 and in each term it is incremented by 1.
Example:
Suppose a = 2 n = 5 Then sum of series: (2*2) + (2*3) + (2*4) + (2*5) = 4+6+8+10 = 28
Let’s see different ways to find sum of the series (a*2) + (a*3) + …… + N.
Method-1: Java Program to Find the Sum of Series (a*2) + (a*3) + (a*4) + …… + (a*N) By Using For Loop
Approach:
- Declare an integer variable say ‘
a
’ which holds the value of first “a
” which will be fixed. - Declare an integer variable say ‘
n
’ which holds the last value of second no in the series. - Create Scanner class object.
- Prompt the user to enter values for
a
andn
. - Use a for loop
from i=2 to i<=n
- Inside for loop we will multiply
a*i
and add with 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 a "); int a = s.nextInt(); //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 long sum = 0; for (int i = 2; i <= n; i++) sum += a * i; System.out.println("Sum of the series is " + sum); } }
Output: Enter the value of a 2 Enter the value of last number 'n' 5 Sum of the series is 28
Method-2: Java Program to Find the Sum of Series (a*2) + (a*3) + (a*4) + …… + (a*N) By Using While Loop
Approach:
- Declare an integer variable say ‘
a
’ which holds the value of first “a
” which will be fixed. - Declare an integer variable say ‘
n
’ which holds the last value of second no in the series. - Create Scanner class object.
- Prompt the user to enter values for
a
andn
. - Use a while loop
from i=2 to i<=n
- Inside while loop we will multiply
a*i
and add with 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 a "); int a = s.nextInt(); //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 double sum = 0; int i = 2; while(i <= n) { sum += a*i; i ++; } System.out.println("sum of the series is " + sum); } }
Output: Enter the value of a 2 Enter the value of last number 'n' 5 sum of the series is 28.0
Method-3: Java Program to Find the Sum of Series (a*2) + (a*3) + (a*4) + …… + (a*N) By Using User Defined Method
Approach:
- Declare an integer variable say ‘
a
’ which holds the value of first “a
” which will be fixed. - Declare an integer variable say ‘
n
’ which holds the last value of second no in the series. - Create Scanner class object.
- Prompt the user to enter value for
a
andn
. - Then call a user defined method by passing
a
andn
. - Use a for loop
from i=2 to i<=n
- Inside for loop we will multiply
a*i
and add with 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 a "); int a = s.nextInt(); //Taking input of number of elements in the series System.out.println("Enter the value of last number 'n' "); int n = s.nextInt(); // calling m1 method to print the series m1(a,n); } public static void m1(int a, int n) { //for loop to print the series long sum = 0; for (int i = 2; i <= n; i++) sum += a * i; System.out.println("Sum of the series is " + sum); } }
Output: Enter the value of a 5 Enter the value of last number 'n' 10 Sum of the series is 270
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Related Java Programs: