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

Here is a C program to find the sum of 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 (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.


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.


#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);

 return 0;
}
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

Related Topics
C Program to generate geometric progression(GP) series
C Program to generate harmonic progression(HP) series
C program to find all roots of quadratic equation
C program to convert decimal numbers to binary numbers
C program to multiply two numbers without using arithmetic operators
C program to convert temperature from celsius to fahrenheit
C program to convert binary number to decimal number system
C program to convert octal number to binary number system
C program to make a simple calculator using switch statement
List of all C programs