C program to find sum of seris in A.P - My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Saturday, December 7, 2019

C program to find sum of seris in A.P - My CS Tutorial

In this tutorial we will learn how to write a program to find sum of series in A.P.

We will enter first two numbers for finding common difference and the last term of series.


C program code


#include<stdio.h>
#include<conio.h>
void main()
{
  int a,n,sum=0,num1,num2,diff,lastnum,series;

  printf("\n Enter first 2 numbers for finding common Difference:\n ");
  scanf("%d%d",&num1,&num2);
  printf("\n Enter last term of series:\n ");
  scanf("%d",&lastnum);
  diff=num2-num1;
  series=num1;

  n=((lastnum-num1)/diff)+1;
  printf("\n Number of terms in series is %d\n ",n);

  for(a=1;a<=n;a++)
  {
   sum+=series;
   series+=diff;
  }

  series=num1;
  printf("\n Sum of the series:\n ");
    for(a=1;a<=n;a++)
  {
     if(a<n)
     {
      printf("%d+",series);
      series+=diff;
     }
     else
     {
      printf("%d=%d\n",series,sum);
      series+=diff;
     }
  }
  getch();
}


OUTPUT:





In this output the value of first two numbers for finding common difference is 1 and 4
And the last term of series is 48

The series is:

1+4+7+10+13+16+19+22+25+28+31+34+37+40+43+46

Numbers of terms in this series is 16

Sum of this series is :-  376


Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.

Created by-- HARSH CHAUHAN

No comments:

Post a Comment