Write a c program to find out the sum of in A.P. series

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

int main(){

    int a,d,n,i,tn;
    int sum=0;

    printf("Enter the first number of the A.P. series: ");
    scanf("%d",&a);

    printf("Enter the total numbers in the A.P. series: ");
    scanf("%d",&n);

    printf("Enter the common difference of A.P. series: ");
    scanf("%d",&d);

    sum = ( n * ( 2 * a + ( n -1 ) * d ) )/ 2;
    tn = a + (n-1) * d;
    printf("Sum of the series A.P.: ");

    for(i=a;i<=tn; i= i + d ){
         if (i != tn)
             printf("%d + ",i);
         else
             printf("%d = %d ",i,sum);
    }

    return 0;
}


Sample output:


Enter the first number of the A.P. series: 1
Enter the total numbers in the A.P. series: 5
Enter the common difference of A.P. series: 3
Sum of the series: 1 + 4 + 7 + 10 + 13 = 35


Definition of arithmetic progression (A.P.):

A series of numbers in which difference of any two consecutive numbers is always a same number that is constant. This constant is called as common difference.

Example of A.P. series:
5 10 15 20 25 …

Here common difference is 5 since difference of any two consecutive numbers for example 20 – 15 or 25 -20 is 5.

Sum of A.P. series:
Sn = n/2(2a + (n-1) d)

Tn term of A.P. series:

Tn = a + (n-1) d 

No comments:

Post a Comment