IP-03.c (574B)
1 /* Write a program to compute the sum of the first n terms of the series using a function: 2 S=1−2+3−4+5−6+… */ 3 4 #include <stdio.h> 5 6 int sum_of_series(int); 7 8 int main() 9 { 10 int n; 11 printf("Enter the n: "); 12 scanf("%d", &n); 13 printf("\nSum of the first %d terms of the series = %d", n, sum_of_series(n)); 14 return 0; 15 } 16 17 int sum_of_series(int n) 18 { 19 int sum = 0; 20 int i; 21 for (i = 1; i <= n; i++) 22 { 23 if (i % 2 == 0) 24 { 25 sum -= i; 26 } 27 else 28 { 29 sum += i; 30 } 31 } 32 return sum; 33 }