In this blog, we will understand to calculate the sum of AP series in C. AP stands for Arithmetic progression. Arithmetic progression is a sequence of terms in which the difference between any two consecutive numbers is the same. The difference between any two consecutive numbers of the series is called the common difference.

Now, we will see formulas used to calculate the sum of AP series.

The nth term in an AP series can be found out using the following formula:

Tn = a + (n-1)d

Here, a is the first term of the AP Series, d is the common difference.

The sum of n elements in an AP series can be expressed as

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

Here, a is the first term of AP and d is the common difference.

The sum of n terms of the AP series can be also be found out if we don't know the common difference, but we should know the last element of the series. The sum of the n elements, in that case, will be

S(n) = n*(a + l)/2, where a is the first term of the AP series and l is the nth or last term of the series.

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

C program to find the sum of AP Series

We will see different ways to find the sum of the AP series in the following examples

Example1

In this example, we calculate the sum of the series by finding each term of the series and adding that to our final sum.

Algorithm

Start

Declare the variables a,n,d for the first term, total elements in AP series and common difference respectively.

Take the value of the first term, the total number of terms, and the common difference from the user.

Declare two variables for sum and tn(for the last number in the series).

Use a for loop that will calculate the sum.

Update the sum in each iteration

In the end, display the calculated sum.

Stop

#include <stdio.h>
int main() {
int a, n, d, tn;
int sum = 0;
printf(" Enter First Number of an A.P Series: ");
scanf("%d", &a);
printf(" Enter the Total Numbers in this A.P Series: ");
scanf("%d", &n);
printf(" Enter the Common Difference: ");
scanf("%d", &d);
// tn stores the nth term of the series
tn = a + (n - 1) * d;
printf("\n The Sum of A.P. Series : ");
// this loop will break when we will reach to last term
for(int i = a; i <= tn; i = i + d)
{
sum+=i;
if(i != tn)
printf("%d + ", i);
else
printf("%d = %d", i, sum);
}
printf("\n");
return 0;
}

Output

Example2

In this example, we calculate the sum of the AP series using the formula that we have discussed in the Formulas section of this article.

Algorithm

Start

Declare the variables a,n,d for the first term, total elements in AP series, and common difference respectively.

Take the value of the first term, the total number of terms, and the common difference from the user.

Declare a variable for sum.

Use the AP sum formula to find the sum.

In the end, display the calculated sum.

Stop

#include <stdio.h>
int main() {
int a, n, d;
int sum = 0;
printf(" Enter First Number of an A.P Series: ");
scanf("%d", &a);
printf(" Enter the Total Numbers in this A.P Series: ");
scanf("%d", &n);
printf(" Enter the Common Difference: ");
scanf("%d", &d);
// AP series sum formula
sum = (n * (2 * a + (n - 1) * d)) / 2;
printf("\n The Sum of Arithmetic Progression Series is = %d\n", sum);
return 0;
}

Output

Letâ€™s move to the FAQ section to clear our minor doubts.

FAQs

What is an AP series? Arithmetic Progression (AP) is a sequence of numbers in which the difference of any two consecutive numbers is a constant value. It is also called Arithmetic Sequence.

How do you find the sum of AP using the last term? If a is the first term in the arithmetic progression, d is the common difference, and the last term is l. We can calculate the sum by given formula: Sum = l + (l â€“ d) + (l â€“ 2d) + (l â€“ 3d) + â€¦

How do you find total terms in AP? We can find the number of terms in an AP by dividing the difference between the last and first terms by the common difference of the AP, and then add 1 to the result we got.

What is the difference between AP and GP? An arithmetic progression (AP) is a sequence of numbers in which each succeeding number is obtained by adding a specific number called the common difference. Whereas a geometric progression (GP) is a sequence of numbers in which each succeeding number is obtained by multiplying a specific number called the common ratio.

How is AP used in real life? Arithmetic progression can be seen in real life by analyzing a certain pattern. For example, AP is used in straight-line depreciation. AP is used to predict any sequence, like when someone is waiting for a cab. Assume that the traffic is moving at a constant speed. They can predict when the next cab will come.

Key Takeaways

In this article, we extensively discussed Arithmetic progression. We have seen different formulas to calculate the sum of the AP series using different formulas. In the end, we have discussed C programs that give the sum of the AP series. Check out Aptitude Questions to practice.

We hope that this blog has helped you enhance your knowledge regarding the sum of Arithmetic progression and if you want to learn more, check out articles on Coding Ninjas Studio. Do upvote our blogs to help other ninjas grow.