C program to find the sum of arithmetic progression series

An arithmetic progression (A.P.) is a sequence of numbers where each term after the first is obtained by adding a constant value called the common difference. This C program calculates the sum of an A.P. series using the mathematical formula.

Syntax

Sum of A.P. Series: S_n = n/2 * (2a + (n - 1) * d)
nth term of A.P. Series: T_n = a + (n - 1) * d

Where:

  • a = first term
  • n = number of terms
  • d = common difference
  • S_n = sum of n terms

Example: Calculate A.P. Sum

This program takes the first term, number of terms, and common difference as input, then calculates and displays the sum of the arithmetic progression series −

#include <stdio.h>

int main() {
    int a, num, diff, tn, i;
    int sum = 0;
    
    printf("Enter 1st number of series: ");
    scanf("%d", &a);
    
    printf("Enter total numbers in series: ");
    scanf("%d", &num);
    
    printf("Enter common difference: ");
    scanf("%d", &diff);
    
    /* Calculate sum using formula */
    sum = (num * (2 * a + (num - 1) * diff)) / 2;
    
    /* Calculate last term */
    tn = a + (num - 1) * diff;
    
    printf("\nSum of A.P series is: ");
    
    /* Display the series */
    for(i = a; i <= tn; i = i + diff) {
        if(i != tn)
            printf("%d + ", i);
        else
            printf("%d = %d", i, sum);
    }
    
    printf("<br>");
    return 0;
}
Enter 1st number of series: 3
Enter total numbers in series: 5
Enter common difference: 4
Sum of A.P series is: 3 + 7 + 11 + 15 + 19 = 55

How It Works

  1. The program reads the first term (a), number of terms (num), and common difference (diff)
  2. It calculates the sum using the formula: S_n = n/2 * (2a + (n-1) * d)
  3. The last term is found using: T_n = a + (n-1) * d
  4. A loop displays each term in the series with proper formatting

Key Points

  • The formula directly calculates the sum without iterating through each term
  • Time complexity is O(n) for displaying the series, O(1) for calculation
  • Space complexity is O(1) as only a few variables are used

Conclusion

This program efficiently calculates the sum of an arithmetic progression using the mathematical formula. It provides both the calculation result and a visual representation of the series for better understanding.

Updated on: 2026-03-15T13:50:01+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements