bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / practice-c / pc-ip-03.c

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 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror