# C Programs To Find Sum of an AP Series

AP stands for **arithmetic progression**. It is defined as a sequence of numbers such that the difference between the consecutive terms is constant. For instance, 2, 4, 6, 8, .... are in AP as the difference between each consecutive number is 2. This constant difference is often referred to as the common difference. We denote the first term of the series as **a,** last term as **tn**, the total number of elements as **n,** and common difference as **d**.

In this example, we are given an arithmetic series and our task is to find the sum of that series. But before moving forward, if you are not familiar with the concept of loops in C, then do check the article on Loops in C.

Input: 2 4 6 8 10

Output: 30

## Program 1: Find the Sum of an A. P. Series

In this method, we will find the sum of an arithmetic series using a for loop. Firstly, the first term, the total number of terms, and the common difference are declared. Then, we calculate the total sum of the arithmetic series using the formula and print it using the for loop.

### Algorithm

- Start
- Declare the variables.
- Initialize the first term, the total number of terms, and the common difference.
- Use a for loop that will calculate the sum.
- Declare the formula for sum and last term before the loop.
- Calculate the sum till the last element in the for loop.
- Display the sum.
- Stop

The below program demonstrates how to calculate the sum of an AP series using for loop.

```
/* C Program to find Sum of Arithmetic Progression Series */
#include <stdio.h>
int main() {
int a, n, d, tn, i; //Variable Declaration
int sum = 0; //Sum declaration and initialization
printf("Enter First Number of an A.P Series:\n");
scanf("%d", &a); //First element initialization
printf("Enter the Total Numbers in this A.P Series:\n");
scanf("%d", &n); //total number of elements initialization
printf("Enter the Common Difference:\n");
scanf("%d", &d); //Common difference initialization
sum = (n * (2 * a + (n - 1) * d)) / 2; //total sum Formula
tn = a + (n - 1) * d; //Last term formula
printf("\nThe Sum of Series A.P. :\n ");
for(i = a; i <= tn; i = i + d)
{
if(i != tn)
printf("%d + ", i);
else
printf("%d = %d", i, sum);
}
printf("\n");
return 0;
}
```

Enter First Number of an A.P Series: 1

Enter the Total Numbers in this A.P Series: 10

Enter the Common Difference: 2

The Sum of Series A.P. :

1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 = 100

## Program 2: Find the Sum of an A. P. Series

In this method, we will find the sum of an arithmetic series using a while loop. Firstly, the first term, the total number of terms, and the common difference are declared. Then, we calculate the total sum of the arithmetic series using the formula and print it using the while loop.

### Algorithm

- Start
- Declare the variables.
- Initialize the first term, the total number of terms, and the common difference.
- Use a while loop that will calculate the sum.
- Declare the formula for sum and last term before the loop.
- Calculate the sum till the last element in the while loop.
- Display the sum.
- Stop

The below program demonstrates how to calculate the sum of an AP series using a while loop.

```
/* C Program to find Sum of Arithmetic Progression Series */
#include <stdio.h>
int main()
{
int a, n, d, tn; //Variable Declaration
int sum=0;
printf("Enter First Number of an A.P Series:\n");
scanf("%d", &a); //First element initialization
printf("Enter the Total Numbers in this A.P Series:\n");
scanf("%d", &n); //total number of elements initialization
printf("Enter the Common Difference:\n");
scanf("%d", &d); //Common difference initialization
sum = (n * (2 * a + (n - 1) * d)) / 2; //Total sum Formula
tn = a + (n - 1) * d; //Last term Formula
int i=a;
while(i <= tn) //To iterate through each element
{
if(i != tn)
printf("%d + ", i);
else
printf("%d = %d", i, sum);
i = i + d; //Increment after each iteration
}
printf("\n");
return 0;
}
```

Enter First Number of an A.P Series: 5

Enter the Total Numbers in this A.P Series: 10

Enter the Common Difference: 3

5 + 8 + 11 + 14 + 17 + 20 + 23 + 26 + 29 + 32 = 185

## Program 3: Find the Sum of an A. P. Series

In this method, we will find the sum of an arithmetic series without using both formula and functions. Firstly, the first term, the total number of terms, and the common difference are declared. Then, we declare two variables; one for sum and the other for the number. In each iteration we will keep on updating both the elements and at the end will print the result.

### Algorithm

- Start
- Declare the variables.
- Initialize the first term, the total number of terms, and the common difference.
- Use a for loop that will calculate the sum.
- Declare two variables for sum and element.
- Update both the elements in each iteration
- At the end display the calculated sum.
- Stop

The below program demonstrates how to calculate the sum of an AP series using for loop without using a formula. Here, we will update both the sum and element in each iteration.

```
/* C Program to find Sum of Arithmetic Progression Series */
#include <stdio.h>
int main()
{
int a, n, d, temp; //Variable Declaration
int sum=0;
printf("Enter First Number of an A.P Series:\n");
scanf("%d", &a); //First element initialization
printf("Enter the Total Numbers in this A.P Series:\n");
scanf("%d", &n); //total number of elements initialization
printf("Enter the Common Difference:\n");
scanf("%d", &d); //Common difference initialization
//Find the sum without using formula
temp = a;
printf("\nThe elements in the series are :\n");
for(int i = 0; i < n; i++)
{
printf("%d ", temp);
sum = sum + temp; //After each element update the sum
temp = temp + d; //After each iteration update the number
}
printf("\n");
//Print the sum of the entire series
printf("\nThe Sum of all the elements in the series is %d\n", sum);
return 0;
}
```

Enter First Number of an A.P Series:

7

Enter the Total Numbers in this A.P Series:

10

Enter the Common Difference:

2

The elements in the series are :

7 9 11 13 15 17 19 21 23 25

The Sum of all the elements in the series is 160

## Program 4: Find the Sum of an A. P. Series

In this method, we will find the sum of an arithmetic series using both formulas and functions. Firstly, the first term, the total number of terms, and the common difference are declared. Then, a function is called to calculate the total sum of the arithmetic series.

### Algorithm

- Start
- Declare the variables.
- Initialize the first term, the total number of terms, and the common difference.
- Call the function that will calculate the sum.
- Declare the formula for sum and last term in the function.
- Calculate the sum till the last element.
- Display the sum.
- Stop

The below program demonstrates how to calculate the sum of an AP series using both functions and formulas.

```
/* C Program to find Sum of Arithmetic Progression Series */
#include <stdio.h>
void findSum(int a, int n, int d);
int main()
{
int a, n, d; //Variable Declaration
printf("Enter First Number of an A.P Series:\n");
scanf("%d", &a); //First element initialization
printf("Enter the Total Numbers in this A.P Series:\n");
scanf("%d", &n); //total number of elements initialization
printf("Enter the Common Difference:\n");
scanf("%d", &d); //Common difference initialization
findSum(a,n,d); //Function Call
return 0;
}
void findSum(int a, int n, int d) //Function Definition
{
int sum = 0; //Sum declaration and initialization
int tn; //Variable for last term Declaration
sum = (n * (2 * a + (n - 1) * d)) / 2; //total sum Formula
tn = a + (n - 1) * d; //Last term formula
printf("\nThe Sum of Series A.P. :\n ");
for(int i = a; i <= tn; i = i + d)
{
if(i != tn)
printf("%d + ", i);
else
printf("%d = %d", i, sum);
}
printf("\n");
}
```

Enter First Number of an A.P Series: 1

Enter the Total Numbers in this A.P Series: 10

Enter the Common Difference: 2

The Sum of Series A.P. :

1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 = 100