Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Formulas
3.
C program to find the sum of AP Series
3.1.
Example1
3.2.
Example2
4.
FAQs
5.
 Key Takeaways
Last Updated: Mar 27, 2024
Easy

C Program For Sum of AP Series

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

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.

Also see: C Static Function, and Tribonacci Series

Formulas

  • 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.

You can also read about dynamic array in c and Short int in C Programming

Get the tech career you deserve, faster!
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

  1. Start
  2. Declare the variables a,n,d for the first term, total elements in AP series and common difference respectively.
  3. Take the value of the first term, the total number of terms, and the common difference from the user.
  4. Declare two variables for sum and tn(for the last number in the series).
  5. Use a for loop that will calculate the sum.
  6. Update the sum in each iteration
  7. In the end, display the calculated sum.
  8. 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

  1. Start
  2. Declare the variables a,n,d for the first term, total elements in AP series, and common difference respectively.
  3. Take the value of the first term, the total number of terms, and the common difference from the user.
  4. Declare a variable for sum.
  5. Use the AP sum formula to find the sum.
  6. In the end, display the calculated sum.
  7. 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

  1. 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.
  2. 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) + …
  3. 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.
  4. 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.
  5. 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.

Check out this problem - Two Sum Problem

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. 

Happy Coding!

Previous article
Total, average and % of 5 subjects in C
Next article
Sum of Geometric Progression Series
Live masterclass