Advertisement Area

Program #14: To Find Sum of series 1+2+3+......n using a Function

C++ Language

#include<iostream.h>
#include<conio.h>
void main()
{
    int series(int);
    int n,sum;
    clrscr();
    cout<<"Enter last term of the series - ";
    cin>>n;
    sum=series(n);
    cout<<"\nSum of series - "<<sum;
    getch();
}
int series(int last)
{
    int i,s=0;
    for(i=1;i<=last;i++)
    s+=i;
    return(s);
}

/*OUTPUT*/
Enter last term of the series - 10
Sum of series - 55