C program to Find the Sum of Arithmetic Progression Series

«
»

This is a C Program to find the sum of A.P series.

Problem Description

This C Program calculates the sum of A.P series.

Problem Solution

This program is used to find the sum of the arithmetic progression series. Here A.P stands for arithmetic progression. A sequence of terms each of which, after the first, is derived by adding to the preceding one a common difference: 5, 9, 13, 17, etc. forms an arithmetic progression. The formula used in thids program arel = a + (n – 1)d . where l is the last term of a finite sequence. Sn = n/2(2a + (n-1) d) where Sn is the sum of n terms.

Program/Source Code

Here is source code of the C Program to Find the the sum of A.P series. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Find the Sum of A.P Series
 */
#include <stdio.h>
#include <math.h>
 
int main()
{
     int a, d, n, i, tn;
     int sum = 0;
 
     printf("Enter the first term value of the A.P. series: ");
     scanf("%d", &a);
     printf("Enter the total numbers in the A.P. series: ");
     scanf("%d", &n);
     printf("Enter the common difference of A.P. series: ");
     scanf("%d", &d);
     sum = (n * (2 * a + (n - 1)* d ))/ 2;
     tn = a + (n - 1) * d;
     printf("Sum of the A.P series is: ");
     for (i = a; i <= tn; i = i + d )
     {
          if (i != tn)
               printf("%d + ", i);
          else
               printf("%d = %d ", i, sum);
     }
     return 0;
}
Program Explanation

In this C program, we are reading the first term of the A.P. series using ‘a’ variable and the total numbers in the A.P. Series using ‘n’ variable and the common difference of the A.P. Series using ‘d’ variable.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

An arithmetic progression is a sequence of terms each of which, after the first, is derived by adding to the preceding one a common difference: 5, 9, 13, 17, etc. forms an arithmetic progression. The formula used in this program is

l = a + (n – 1) d,

Where l is the last term of a finite sequence.

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

Where, Sn is the sum of n terms.

Runtime Test Cases
 
Output:
$cc pgm21.c
$ a.out
Enter the first term value of the A.P. series: 1
Enter the total numbers in the A.P. series: 5
Enter the common difference of A.P. series: 2
Sum of the A.P series is: 1 + 3 + 5 + 7 + 9 = 25

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.