×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to find the sum of the Arithmetic Progression (A.P.) series

Here, we are going to learn how to find the sum of the Arithmetic Progression (A.P.) series using C program?
Submitted by Nidhi, on August 01, 2021

Arithmetic Progression (AP)

A series of numbers is called an arithmetic progression (AP) series if the difference between any two consecutive terms is always the same.

Example: 2 4 6 8 10 ...

In the below program, we will read the first term, the total number of terms, and common difference from the user and print A.P. series and its sum.

C program to find the sum of the Arithmetic Progression (A.P.) series

The source code to find the sum of the Arithmetic Progression (A.P.) series is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to find the sum of
// Arithmetic Progression (A.P.) series

#include <stdio.h>
#include <math.h>

int main()
{
    int sum = 0;

    int a = 0;
    int n = 0;
    int d = 0;
    int i = 0;
    int t = 0;

    printf("Enter the first term: ");
    scanf("%d", &a);

    printf("Enter the value of n: ");
    scanf("%d", &n);

    printf("Enter the difference: ");
    scanf("%d", &d);

    sum = (n * (2 * a + (n - 1) * d)) / 2;
    t = a + (n - 1) * d;

    printf("Sum of the A.P series: ");
    i = a;
    while (i <= t) {
        if (i == t) {
            printf("%d = %d ", i, sum);
            break;
        }

        printf("%d + ", i);

        i = i + d;
    }

    printf("\n");
    return 0;
}

Output

RUN 1:
Enter the first term: 2
Enter the value of n: 10
Enter the difference: 2
Sum of the A.P series: 2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 = 110

RUN2:
Enter the first term: 100
Enter the value of n: 5
Enter the difference: 3
Sum of the A.P series: 100 + 103 + 106 + 109 + 112 = 530 

RUN 3:
Enter the first term: 10
Enter the value of n: 10
Enter the difference: 5
Sum of the A.P series: 10 + 15 + 20 + 25 + 30 + 35 + 40 + 45 + 50 + 55 = 325

Explanation

Here, we read the first term, common difference, and the total number of terms from the user. Then we generated the A.P. series and find its total and printed them on the console screen.

C Sum of Series Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.