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