Program to calculate the sum of the series:
1-2+3-4+5-6+7-8.....N
Solution using C language:
#include <stdio.h>
//function for creating the sum of the
//series up to Nth term
int series_sum(int n)
{
if (n % 2 == 0)
return (-(n / 2));
else
return ((n + 1) / 2);
}
// main code
int main()
{
int n;
printf("Series:1-2+3-4+5-6+7-8.....N\n");
printf("Want some up to N terms?\nEnter the N term:");
scanf("%d", &n);
printf("Sum is:%d", series_sum(n));
return 0;
}
Output:
Series:1-2+3-4+5-6+7-8.....N
Want some up to N terms?
Enter the N term:10
Sum is:-5
1-2+3-4+5-6+7-8.....N
Solution using C language:
#include <stdio.h>
//function for creating the sum of the
//series up to Nth term
int series_sum(int n)
{
if (n % 2 == 0)
return (-(n / 2));
else
return ((n + 1) / 2);
}
// main code
int main()
{
int n;
printf("Series:1-2+3-4+5-6+7-8.....N\n");
printf("Want some up to N terms?\nEnter the N term:");
scanf("%d", &n);
printf("Sum is:%d", series_sum(n));
return 0;
}
Output:
Series:1-2+3-4+5-6+7-8.....N
Want some up to N terms?
Enter the N term:10
Sum is:-5
No comments:
Post a Comment