Program for sum of arithmetic series
Last Updated :
16 Feb, 2023
A series with the same common difference is known as arithmetic series. The first term of series is a and the common difference is d. The series looks like a, a + d, a + 2d, a + 3d, . . . Task is to find the sum of the series.
Examples:
Input : a = 1
d = 2
n = 4
Output : 16
1 + 3 + 5 + 7 = 16
Input : a = 2.5
d = 1.5
n = 20
Output : 335
A simple solution to find the sum of arithmetic series.
C++
// CPP Program to find the sum of arithmetic
// series.
#include<bits/stdc++.h>
using namespace std;
// Function to find sum of series.
float sumOfAP(float a, float d, int n)
{
float sum = 0;
for (int i=0;i<n;i++)
{
sum = sum + a;
a = a + d;
}
return sum;
}
// Driver function
int main()
{
int n = 20;
float a = 2.5, d = 1.5;
cout<<sumOfAP(a, d, n);
return 0;
}
Java
// JAVA Program to find the sum of
// arithmetic series.
class GFG{
// Function to find sum of series.
static float sumOfAP(float a, float d,
int n)
{
float sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + a;
a = a + d;
}
return sum;
}
// Driver function
public static void main(String args[])
{
int n = 20;
float a = 2.5f, d = 1.5f;
System.out.println(sumOfAP(a, d, n));
}
}
/*This code is contributed by Nikita Tiwari.*/
Python
# Python Program to find the sum of
# arithmetic series.
# Function to find sum of series.
def sumOfAP( a, d,n) :
sum = 0
i = 0
while i < n :
sum = sum + a
a = a + d
i = i + 1
return sum
# Driver function
n = 20
a = 2.5
d = 1.5
print (sumOfAP(a, d, n))
# This code is contributed by Nikita Tiwari.
C#
// C# Program to find the sum of
// arithmetic series.
using System;
class GFG {
// Function to find sum of series.
static float sumOfAP(float a, float d,
int n)
{
float sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + a;
a = a + d;
}
return sum;
}
// Driver function
public static void Main()
{
int n = 20;
float a = 2.5f, d = 1.5f;
Console.Write(sumOfAP(a, d, n));
}
}
// This code is contributed by parashar.
PHP
<?php
// PHP Program to find the sum
// of arithmetic series.
// Function to find sum of series.
function sumOfAP($a, $d, $n)
{
$sum = 0;
for ($i = 0; $i < $n; $i++)
{
$sum = $sum + $a;
$a = $a + $d;
}
return $sum;
}
// Driver Code
$n = 20;
$a = 2.5; $d = 1.5;
echo(sumOfAP($a, $d, $n));
// This code is contributed by Ajit.
?>
JavaScript
<script>
// Javascript Program to find the sum of arithmetic
// series.
// Function to find sum of series.
function sumOfAP(a, d, n)
{
let sum = 0;
for (let i=0;i<n;i++)
{
sum = sum + a;
a = a + d;
}
return sum;
}
// Driver function
let n = 20;
let a = 2.5, d = 1.5;
document.write(sumOfAP(a, d, n));
// This code is contributed by Mayank Tyagi
</script>
Output:
335
Time Complexity: O(n)
Space complexity: O(1) because using constant space
Approach 2:
An Efficient solution to find the sum of arithmetic series is to use the below formula as follows:
Sum of arithmetic series
= ((n / 2) * (2 * a + (n - 1) * d))
Where
a - First term
d - Common difference
n - No of terms
Example:
C++
// Efficient solution to find sum of arithmetic series.
#include<bits/stdc++.h>
using namespace std;
float sumOfAP(float a, float d, float n)
{
float sum = (n / 2) * (2 * a + (n - 1) * d);
return sum;
}
// Driver code
int main()
{
float n = 20;
float a = 2.5, d = 1.5;
cout<<sumOfAP(a, d, n);
return 0;
}
Java
// Java Efficient solution to find
// sum of arithmetic series.
class GFG
{
static float sumOfAP(float a, float d, float n)
{
float sum = (n / 2) * (2 * a + (n - 1) * d);
return sum;
}
// Driver code
public static void main (String[] args)
{
float n = 20;
float a = 2.5f, d = 1.5f;
System.out.print(sumOfAP(a, d, n));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 Efficient
# solution to find sum
# of arithmetic series.
def sumOfAP(a, d, n):
sum = (n / 2) * (2 * a + (n - 1) * d)
return sum
# Driver code
n = 20
a = 2.5
d = 1.5
print(sumOfAP(a, d, n))
# This code is
# contributed by sunnysingh
C#
// C# efficient solution to find
// sum of arithmetic series.
using System;
class GFG {
static float sumOfAP(float a,
float d,
float n)
{
float sum = (n / 2) *
(2 * a +
(n - 1) * d);
return sum;
}
// Driver code
static public void Main ()
{
float n = 20;
float a = 2.5f, d = 1.5f;
Console.WriteLine(sumOfAP(a, d, n));
}
}
// This code is contributed by Ajit.
PHP
<?php
// Efficient PHP code to find sum
// of arithmetic series.
// Function to find sum of series.
function sumOfAP($a, $d, $n)
{
$sum = ($n / 2) * (2 * $a +
($n - 1) * $d);
return $sum;
}
// Driver code
$n = 20;
$a = 2.5; $d = 1.5;
echo(sumOfAP($a, $d, $n));
// This code is contributed by Ajit.
?>
JavaScript
// Efficient solution to find sum of arithmetic series.
function sumOfAP(a, d, n) {
let sum = (n / 2) * (2 * a + (n - 1) * d);
return sum;
}
// Driver code
let n = 20;
let a = 2.5, d = 1.5;
document.write(sumOfAP(a, d, n));
// This code is contributed by Ashok
Time Complexity: O(1)
Space complexity: O(1) since using only constant variables
How does this formula work?
We can prove the formula using mathematical induction. We can easily see that the formula holds true for n = 1 and n = 2. Let this be true for n = k-1.
Let the formula be true for n = k-1.
Sum of first k - 1 elements of arithmetic series is
= (((k-1))/ 2) * (2 * a + (k - 2) * d))
We know k-th term of arithmetic series is
= a + (k - 1)*d
Sum of first k elements =
= Sum of (k-1) numbers + k-th element
= (((k-1)/2)*(2*a + (k-2)*d)) + (a + (k-1)*d)
= [((k-1)(2a + (k-2)d) + (2a + 2kd - 2d)]/2
= ((k / 2) * (2 * a + (k - 1) * d))
Similar Reads
Program for sum of geometric series A Geometric series is a series with a constant ratio between successive terms. The first term of the series is denoted by a and common ratio is denoted by r. The series looks like this :- a, ar, ar2, ar3, ar4, . . .. The task is to find the sum of such a series. Examples :Input : a = 1 r = 0.5 n = 3
8 min read
Program to find sum of harmonic series Harmonic series is inverse of a arithmetic progression. In general, the terms in a harmonic progression can be denoted as 1/a, 1/(a + d), 1/(a + 2d), 1/(a + 3d) .... 1/(a + nd). As Nth term of AP is given as ( a + (n â 1)d). Hence, Nth term of harmonic progression is reciprocal of Nth term of AP, wh
5 min read
Program to print Arithmetic Progression series Given first term (a), common difference (d) and a integer n of the Arithmetic Progression series, the task is to print the series. Examples : Input : a = 5, d = 2, n = 10Output : 5 7 9 11 13 15 17 19 21 23Approach : We know the Arithmetic Progression series is like = 2, 5, 8, 11, 14 â¦. ⦠In this ser
4 min read
Sum of Arithmetic Geometric Sequence In mathematics, an arithmeticoâgeometric sequence is the result of the term-by-term multiplication of a geometric progression with the corresponding terms of an arithmetic progression. is an arithmeticoâgeometric sequence.Given the value of a(First term of AP), n(Number of terms), d(Common Differenc
9 min read
Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n Given a positive integer n and the task is to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n. Examples: Input : n = 5 Output : 55 = 1 + 2 + 2 + 3 + 3 + 3 + 4 + 4 + 4 + 4 + 5 + 5 + 5 + 5 + 5. = 55 Input : n = 10 Output : 385 Addition method: In addition method sum all the elements one by one. B
9 min read