C program to find out the sum of given A.P

source code


#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);
    }
    gech();
    return 0;
}

Comments

Popular posts from this blog

8086 STRING MANIPULATION –FIND AND REPLACE A WORD

Animated Circles In C++

C program to find out the sum of series 1 + 2 + …. + n.