C Program to Print Arithmetic Progression (AP) Series and Sum till N Terms

  • Write a C program to find the sum of arithmetic series till N terms.
  • Write a C program to print arithmetic series till N terms.

Arithmetic series is a sequence of terms in which next term is obtained by adding common difference to previous term. Let, tn be the nth term of AP, then (n+1)th term of can be calculated as <br\>(n+1)th = tn + D
where D is the common difference (n+1)th – tn
The formula to calculate Nth term tn = a + (n – 1)d;
where, a is first term of AP and d is the common difference.</br\>

C program to print arithmetic progression series and it’s sum till N terms

In this program, we first take number of terms, first term and common difference as input from user using scanf function. Then we calculate the arithmetic series using above formula(by adding common difference to previous term) inside a for loop. We keep on adding the current term’s value to sum variable.

C Program to Print Arithmetic Progression(AP) Series and Sum till N Terms

/*
* C program to print Arithmetic Series and it's sum till Nth term
*/
#include <stdio.h>
#include <stdlib.h>
 
int main() {
    int first, diff, terms, value, sum=0, i;
 
    printf("Enter the number of terms in AP series\n");
    scanf("%d", &terms);
 
    printf("Enter first term and common difference of AP series\n");
    scanf("%d %d", &first, &diff);
 
    /* print the series and add all elements to sum */
    value = first;
    printf("AP SERIES\n");
    for(i = 0; i < terms; i++) {
        printf("%d ", value);
        sum += value;
        value = value + diff;
    }
 
    printf("\nSum of the AP series till %d terms is %d\n", terms, sum);
 
    getch();
 return 0;
}

Program Output

Enter the number of terms in AP series
5
Enter first term and common difference of AP series
2 4
AP SERIES
2 6 10 14 18
Sum of the AP series till 5 terms is 50