Sum of Series||C language||programming_info ||Series problem 16

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
Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support