C Program to Find Sum of Arithmetic Progression Series

In this tutorial, i am going to show you how to find sum of arithmetic progression series or AP series with the help of for loop, while loop, and function in c programs.

All C Programs to Find Sum of Arithmetic Progression Series

  • C Program to Find Sum of Arithmetic Progression Series using For Loop
  • C Program to Find Sum of Arithmetic Progression Series using While Loop
  • C Program to Find Sum of Arithmetic Progression Series using Function

C Program to Find Sum of Arithmetic Progression Series using For Loop

/* C Program to find Sum of Arithmetic Progression Series */
#include <stdio.h>
int main() {
    
    int a, n, d, tn, i;
    int sum = 0;
    
    printf(" Please Enter First Number of an A.P Series:  ");
    scanf("%d", &a);
    printf(" Please Enter the Total Numbers in this A.P Series:  ");
    scanf("%d", &n);
    printf(" Please Enter the Common Difference:  ");
    scanf("%d", &d);
    
    sum = (n * (2 * a + (n - 1) * d)) / 2;
    tn = a + (n - 1) * d;
    printf("\n The Sum of Series A.P. : ");
    for(i = a; i <= tn; i = i + d)
    {
        if(i != tn)
            printf("%d + ", i);
        else
            printf("%d = %d", i, sum);
    }
    printf("\n");
    return 0;
}

The result of the above c program; is as follows:

Please Enter First Number of an A.P Series:  1
Please Enter the Total Numbers in this A.P Series:  5
Please Enter the Common Difference:  4
The Sum of Series A.P. : 1 + 5 + 9 + 13 + 17 = 45

C Program to Find Sum of Arithmetic Progression Series using While Loop

/* C Program to find Sum of Arithmetic Progression Series */
#include <stdio.h>
int main() {
    
    int a, n, d, tn, i;
    int sum = 0;
    
    printf(" Please Enter First Number of an A.P Series:  ");
    scanf("%d", &a);
    printf(" Please Enter the Total Numbers in this A.P Series:  ");
    scanf("%d", &n);
    printf(" Please Enter the Common Difference:  ");
    scanf("%d", &d);
    
    sum = (n * (2 * a + (n - 1) * d)) / 2;
    tn = a + (n - 1) * d;
    i = a;
    printf("\n The Sum of Series A.P. : ");
    while(i <= tn)
    {
        if(i != tn)
            printf("%d + ", i);
        else
            printf("%d = %d", i, sum);
        i = i + d;
    }
    printf("\n");
    return 0;
}

The result of the above c program; is as follows:

Please Enter First Number of an A.P Series:  1
Please Enter the Total Numbers in this A.P Series:  6
Please Enter the Common Difference:  5
The Sum of Series A.P. : 1 + 6 + 11 + 16 + 21 + 26 = 81

C Program to Find Sum of Arithmetic Progression Series using Function

/* C Program to find Sum of Arithmetic Progression Series */
#include <stdio.h>
int sumofAP(int a, int n, int d)
{
    int sum = (n * (2 * a + (n - 1) * d)) / 2;
    return sum;
}
int main() {
    
    int a, n, d;
    int sum = 0;
    
    printf(" Please Enter First Number of an A.P Series:  ");
    scanf("%d", &a);
    printf(" Please Enter the Total Numbers in this A.P Series:  ");
    scanf("%d", &n);
    printf(" Please Enter the Common Difference:  ");
    scanf("%d", &d);
    
    sum = sumofAP(a, n, d);
    printf("\n The Sum of Arithmetic Progression Series is =  %d\n", sum);
    return 0;
}

The result of the above c program; is as follows:

Please Enter First Number of an A.P Series:  1
Please Enter the Total Numbers in this A.P Series:  8
Please Enter the Common Difference:  6
The Sum of Arithmetic Progression Series is =  176

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *