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