Sum of square series.(Second method)

Share it Please
This program is used to illustrate 12+22+32+........+n2. In the previous Same program We have used just formula to find the sum of this series but in this program we have manually squared each number and then added to ans variable.In this program we have defined ans=0 because if we have not defined ans=0 then program will pick garbage value which will affect our answer.You can see other version of this program here.

Program:

#include<stdio.h>
#include<conio.h>
main()
{
    long n,ans=0,n2,a;
    clrscr();
    printf("Enter number: ");
    scanf("%ld",&n);
    for(a=1;a<=n;a++)
    {
        n2=a*a;
        ans=ans+n2;
    }
    printf("Sum of square series till %ld is %ld",n,ans);
    getch();
}


output:
Sum of square series in cprogramming