Contact Learn C
Copy Program Share Program

Program 186:Sum of Series 1^2-2^2+3^2-......+n^2

Program 186:
 
 #include <stdio.h>
 #include<math.h>
 main()
 {
  //1^2-2^2+3^2-......+n^2
   int i,num,sum=0;  
   printf("Enter number \n");
   scanf("%d",&num);
   for(i=1;i<=num;i++)
   {
    if(i%2==0)
    {
     sum-=pow(i,2);
     }
     if(i%2==1)
     {
      sum+=pow(i,2);
     }
    
   }
   printf("Sum of series\n");
   for(i=1;i<=num;i++)
   {
    if(i!=num)
    {
     if(i%2==0)
     {
         printf("%d^2+ ",i); 
      }
      else
      {
       printf("%d^2- ",i);
      }
   
     }
     else
     {
      printf("%d^2 = %d\n",i,sum);
     }
 
   }
 }



Explanation:

//Coming Soon...

Output:

Sum of Series 1^2-2^2+3^2-......+n^2






 

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide