An arithmetic sequence is a sequence in which each term differs from the preceding one by a constant quantity. And knowing how to find these can help you build your programming skillset which whichever language(s) you use.
In this article, you'll learn how to find the sum of the arithmetic series using Python, C++, JavaScript, and C.
What Is an Arithmetic Series?
The sum of the terms of a finite arithmetic sequence is called an arithmetic series. The arithmetic sequence is denoted as follows:
a, a+d, a+2d, a+3d, a+4d, ...
where,
a = First term
d = Common difference
Problem Statement
You're given the first term, common difference, and no. of terms of the arithmetic series. You need to find the sum of the arithmetic series. Example: Let firstTerm = 1, commonDifference = 2, and noOfTerms = 5. Arithmetic Series: 1 + 3 + 5 + 7 + 9 Sum of the arithmetic series: 25 Thus, the output is 25.
Iterative Approach to Find the Sum of an Arithmetic Series
First, we'll take a look at the iterative approach. You can find out how to find sums in this way for the main programming languages below.
C++ Program to Find the Sum of an Arithmetic Series Using Iteration
Below is the C++ program to find the sum of an arithmetic series using iteration:
// C++ program to find the sum of arithmetic series
#include <iostream>
using namespace std;
// Function to find the sum of arithmetic series
int sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms)
{
int result = 0;
for (int i=0; i<noOfTerms; i++)
{
result = result + firstTerm;
firstTerm = firstTerm + commonDifference;
}
return result;
}
int main()
{
int firstTerm = 1;
int commonDifference = 2;
int noOfTerms = 5;
cout << "First Term: " << firstTerm << endl;
cout << "Common Difference: " << commonDifference << endl;
cout << "Number of Terms: " << noOfTerms << endl;
cout << "Sum of the arithmetic series: " << sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) << endl;
return 0;
}
Output:
First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25
Python Program to Find the Sum of an Arithmetic Series Using Iteration
Below is the Python program to find the sum of an arithmetic series using iteration:
# Python program to find the sum of arithmetic series
# Function to find the sum of arithmetic series
def sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms):
result = 0
for i in range(noOfTerms):
result = result + firstTerm
firstTerm = firstTerm + commonDifference
return result
firstTerm = 1
commonDifference = 2
noOfTerms = 5
print("First Term:", firstTerm)
print("Common Difference:", commonDifference)
print("Number of Terms:", noOfTerms)
print("Sum of the arithmetic series:", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms))
Output:
First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25
JavaScript Program to Find the Sum of an Arithmetic Series Using Iteration
Below is the JavaScript program to find the sum of an arithmetic series using iteration:
// JavaScript program to find the sum of arithmetic series
// Function to find the sum of arithmetic series
function sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) {
var result = 0;
for (let i=0; i<noOfTerms; i++)
{
result = result + firstTerm;
firstTerm = firstTerm + commonDifference;
}
return result;
}
var firstTerm = 1;
var commonDifference = 2;
var noOfTerms = 5;
document.write("First Term: " + firstTerm + "<br>");
document.write("Common Difference: " + commonDifference + "<br>");
document.write("Number of Terms: " + noOfTerms + "<br>");
document.write("Sum of the arithmetic series: " + sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));
Output:
First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25
C Program to Find the Sum of an Arithmetic Series Using Iteration
Below is the C program to find the sum of an arithmetic series using iteration:
// C program to find the sum of arithmetic series
#include <stdio.h>
// Function to find the sum of arithmetic series
int sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms)
{
int result = 0;
for (int i=0; i<noOfTerms; i++)
{
result = result + firstTerm;
firstTerm = firstTerm + commonDifference;
}
return result;
}
int main()
{
int firstTerm = 1;
int commonDifference = 2;
int noOfTerms = 5;
printf("First Term: %d \n", firstTerm);
printf("Common Difference: %d \n", commonDifference);
printf("Number of Terms: %d \n", noOfTerms);
printf("Sum of the arithmetic series: %d \n", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));
return 0;
}
Output:
First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25
Efficient Approach to Find the Sum of an Arithmetic Series Using Formula
You can use the following formula to find the sum of the arithmetic series:
Sum of arithmetic series = ((n / 2) * (2 * a + (n - 1) * d))
where,
a = First term
d = Common difference
n = No. of terms
C++ Program to Find the Sum of an Arithmetic Series Using Formula
Below is the C++ program to find the sum of an arithmetic series using the formula:
// C++ program to find the sum of arithmetic series
#include <iostream>
using namespace std;
// Function to find the sum of arithmetic series
int sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms)
{
return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference);
}
int main()
{
int firstTerm = 1;
int commonDifference = 2;
int noOfTerms = 5;
cout << "First Term: " << firstTerm << endl;
cout << "Common Difference: " << commonDifference << endl;
cout << "Number of Terms: " << noOfTerms << endl;
cout << "Sum of the arithmetic series: " << sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) << endl;
return 0;
}
Output:
First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25
Python Program to Find the Sum of an Arithmetic Series Using Formula
Below is the Python program to find the sum of an arithmetic series using the formula:
# Python program to find the sum of arithmetic series
# Function to find the sum of arithmetic series
def sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms):
return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference)
firstTerm = 1
commonDifference = 2
noOfTerms = 5
print("First Term:", firstTerm)
print("Common Difference:", commonDifference)
print("Number of Terms:", noOfTerms)
print("Sum of the arithmetic series:", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms))
Output:
First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25
JavaScript Program to Find the Sum of an Arithmetic Series Using Formula
Below is the JavaScript program to find the sum of an arithmetic series using the formula:
// JavaScript program to find the sum of arithmetic series
// Function to find the sum of arithmetic series
function sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) {
return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference);
}
var firstTerm = 1;
var commonDifference = 2;
var noOfTerms = 5;
document.write("First Term: " + firstTerm + "<br>");
document.write("Common Difference: " + commonDifference + "<br>");
document.write("Number of Terms: " + noOfTerms + "<br>");
document.write("Sum of the arithmetic series: " + sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));
Output:
First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25
C Program to Find the Sum of an Arithmetic Series Using Formula
Below is the C program to find the sum of an arithmetic series using the formula:
// C program to find the sum of arithmetic series
#include <stdio.h>
// Function to find the sum of arithmetic series
int sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms)
{
return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference);
}
int main()
{
int firstTerm = 1;
int commonDifference = 2;
int noOfTerms = 5;
printf("First Term: %d \n", firstTerm);
printf("Common Difference: %d \n", commonDifference);
printf("Number of Terms: %d \n", noOfTerms);
printf("Sum of the arithmetic series: %d \n", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));
return 0;
}
Output:
First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25
Finding Arithmetic Series With Different Programming Languages Is Easy
Now you've read this article, you know how to find arithmetic series with each of the main programming languages.
C++ is one of the "bread and butter" programming languages. It's used to develop a variety of software like databases, operating systems, compilers, web browsers, etc. If you're looking to learn C++, you should check out some of the best sites like Udemy, edX, LearnCpp, and so on.