assignment-s-03.c (710B)
1 /* 2 * Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 12 Dec 2025 3 * Repo: https://github.com/notamitgamer/bsc 4 * License: MIT 5 */ 6 7 /* Write a program to compute the sum of the first n terms of the series using a function: 8 S=1−2+3−4+5−6+… */ 9 10 #include <stdio.h> 11 12 int sum_of_series(int); 13 14 int main() 15 { 16 int n; 17 printf("Enter the n: "); 18 scanf("%d", &n); 19 printf("\nSum of the first %d terms of the series = %d", n, sum_of_series(n)); 20 return 0; 21 } 22 23 int sum_of_series(int n) 24 { 25 int sum = 0; 26 int i; 27 for (i = 1; i <= n; i++) 28 { 29 if (i % 2 == 0) 30 { 31 sum -= i; 32 } 33 else 34 { 35 sum += i; 36 } 37 } 38 return sum; 39 }