ADVERTISEMENT
ADVERTISEMENT

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

Problem Solution:

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.

Program:

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.