Print exponential series and its sum - C Program

C program to print the following series using for loop.

1^2+2^2+3^2+4^2...N^2


Solution:

Mathematical formula for the sum of above series is : n*(n+1)*(2n+1)/6

#include<stdio.h>
int main()
{
      int n,i;
      float sum=0;
      printf("1^2+2^2+3^2+4^2...N^2");
      printf("\n\nEnter Value of N : ");
      scanf("%d",&n);
      sum = (n * (n + 1) * (2 * n + 1 )) / 6.0;
      printf("\n");
      for(i =1;i <= n;i++)
      {
            if (i!=n)
                  printf("%d^2 + ",i);
            else
                  printf("%d^2 ",i);
      }
      printf("\n\nSum of Series = %.2f",sum);
      return 0;
}


Output:

exponential series