In this tutorial we will learn how to write a c program to print sum of series 1^2- 2^2+ 3^2- 4^2+ 5^2-..........+n^2.
In this output the value of input number is 8
Sum of series:
1^2- 2^2+ 3^2- 4^2+ 5^2- 6^2+ 7^2- 8^2
And the sum of series is -36
C program code
#include <stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,num,sum=0;
printf("\n Enter number:\n ");
scanf("%d",&num);
for(a=1;a<=num;a++)
{
if(a%2==0)
{
sum-=pow(a,2);
}
if(a%2==1)
{
sum+=pow(a,2);
}
}
printf("\n Sum of series:\n ");
for(a=1;a<=num;a++)
{
if(a!=num)
{
if(a%2==0)
{
printf("%d^2+ ",a);
}
else
{
printf("%d^2- ",a);
}
}
else
{
printf("%d^2 = %d\n",a,sum);
}
}
getch();
}
OUTPUT:
In this output the value of input number is 8
Sum of series:
1^2- 2^2+ 3^2- 4^2+ 5^2- 6^2+ 7^2- 8^2
And the sum of series is -36
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.
Created by-- HARSH CHAUHAN
No comments:
Post a Comment