-Pages-

Calculate Sum of an A.P.

 C++ Program to Calculate sum Of An A.P. series. This program calculates the sum of the an A.P. and prints the result on the compiler screen. The first term 'a', number of terms 'n' and the common difference 'd' are input by the user. Sum of an A.P. is
calculated by the formula : sum=(n*(2*a+(n-1)*d))/2 , nth term by : nth=a+(n-1)*d . For loop is used to print the whole series upto n and the sum is printed on compiler screen


Source Code:-

#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
 int a,d,n,i,nth;
 int sum=0;
 clrscr();
 cout<<"Program to Calculate sum of A.P series by-Tarun Rawat\n";
 cout<<"\nEnter the first number of the series : ";
 cin>>a;
 cout<<"\nEnter the number of terms in series : ";
 cin>>n;
 cout<<"\nEnter the common difference of series : ";
 cin>>d;
 sum=(n*(2*a+(n-1)*d))/2;
 nth=a+(n-1)*d;
 cout<<"\nSum of the A.P. is : ";
 for(i=a;i<=nth;i=i+d)
 {
  if(i!=nth)
  {
    cout<<i<<" + ";
  }
  else
  {
    cout<<i<<" = "<<sum;
  }
 }
 getch();
}

No comments:

Post a Comment