Java Program to Find Sum of Arithmetic Progression

In this article we are going to see find sum of Arithmetic Progression using Java programming language.

Java Program to Find Sum of Arithmetic Progression

The formula to calculate the sum of arithmetic progression is

Sn=n/2(2a+(n-1)d)

where,

  • a = first number the AP
  • n = number of terms in the AP
  • d = common difference between elements

Example:

In an AP if the first term i.e., a = 2, total number of terms i.e., n = 6 and the common difference between the terms i.e., d = 3, then

S

n

 =n/2(2a+(n-1)d)
=6/2 (2 X 2 + (6-1) X 3)
=3(4+15)
= 3 X 19
= 57

We can also obtain the nth term of an AP series by using the formula

T

n

 = a + (n – 1) d

Let’s see different ways to find sum of Arithmetic Progression.

Method-1: Java Program to Find Sum of Arithmetic Progression Using User Input Value Using the Formula

Approach:

  • Take the user input for the first term, common difference, and the number of terms.
  • Use the formula to get the sum of the series and print the result

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the first term of the series: ");
        int a = sc.nextInt();
        System.out.print("Enter the common difference of the series: ");
        int d = sc.nextInt();
        System.out.print("Enter the number of terms in the series: ");
        int n = sc.nextInt();
        int sum = (n * (2 * a + (n - 1) * d)) / 2;
        int tn = a + (n - 1) * d;
        System.out.println("The sum of the series is: ");
        for (int i = a; i <= tn; i += d) 
        {
            if (i != tn)
                System.out.printf("%d + ", i);
            else
                System.out.printf("%d = %d", i, sum);
        }
    }

}
Output:

Enter the first term of the series: 40
Enter the common difference of the series: 4
Enter the number of terms in the series: 15
The sum of the series is: 
40 + 44 + 48 + 52 + 56 + 60 + 64 + 68 + 72 + 76 + 80 + 84 + 88 + 92 + 96 = 1020

Method-2: Java Program to Find Sum of Arithmetic Progression By Using User Input Value Without Using the Sum Formula

Approach:

  • Take the user input for the first term, common difference, and the number of terms.
  • Initialize sum variable as 0.
  • Use a for loop for i = 0 -> n.
  • Inside the for loop update the sum variable as sum += a + i * d
  • Return sum.
  • Print the result.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the first term of the series: ");
        int a = sc.nextInt();
        System.out.print("Enter the common difference of the series: ");
        int d = sc.nextInt();
        System.out.print("Enter the number of terms in the series: ");
        int n = sc.nextInt();
        System.out.println("The sum of the series is: " + calcSum(a, d, n));

    }

    private static int calcSum(int a, int d, int n) 
    {
        int sum = 0;
        for (int i = 0; i < n; i++) 
        {
            sum += a + i * d;
        }
        return sum;
    }

}
Output:

Enter the first term of the series: 40
Enter the common difference of the series: 4
Enter the number of terms in the series: 15
The sum of the series is: 1020

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Leave a Comment