- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find sum of the series 1-2+3-4+5-6+7....in C++
In this problem, we are given an integer value N. Our task is to find Sum of Series 1 - 2 + 3 - 4 + 5 - 6 + 7 upto n terms.
The series is 1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 + 9 - 10...
Let's take an example to understand the problem,
Input : N = 4 Output : -2
Explanation −
1 - 2 + 3 - 4 = -2
Solution Approach
A simple approach to solve the problem is finding the general term of the series and then finding the sum till n terms. And calculating the sum using formula will reduce the time to O(1).
The series is,
1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 + 9 - 10...
Let's find the sum of series for some values,
sum(1) = 1
sum(2) = 1 - 2 = -1
sum(3) = 1 - 2 + 3 = 2
sum(4) = 1 - 2 + 3 - 4 = -2
sum(5) = 1 - 2 + 3 - 4 + 5 = 3
sum(6) = 1 - 2 + 3 - 4 + 5 - 6 = -3
sum(7) = 1 - 2 + 3 - 4 + 5 - 6 + 7 = 4
sum(8) = 1 - 2 + 3 - 4 + 5 - 6 + 7 - 8 = -4
For here, we can conclude that the sum can be formulated as,
Sum = +(n+1)/2 if n is odd.
Sum = -(n)/2 if n is even.
Example
Program to illustrate the working of our solution
#include<iostream> using namespace std; int calcSumNTerms(int n) { if(n%2 == 0) return ((-1)*(n/2)); return ((n+1)/2); } int main() { int n = 156; cout<<"The sum of series upto n terms is "<<calcSumNTerms(n); return 0; }
Output
The sum of series upto n terms is -78
- Related Articles
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)) in C++
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + ...... + (1+3+5+7+...+(2n-1)) in C++
- Sum of series 2/3 – 4/5 + 6/7 – 8/9 + …… upto n terms
- Simplify (i) +7 + 1 - 3 + 4 -2 - 5 + 7 - 2 + 1 -6 + 5(ii)+2 - 3 + 2 -5 + 1 - 3 + 8 - 4 + 3
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- Find the sum of the mode and the median of the given data. 2, 4, 3, 4, 6, 2, 5, 1, 3, 2, 1.
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++
- Solve(a) ( frac{2}{3}+frac{1}{7} )(b) ( frac{3}{10}+frac{7}{15} )(c) ( frac{4}{9}+frac{2}{7} )(d) ( frac{5}{7}+frac{1}{3} )(e) ( frac{2}{5}+frac{1}{6} )(f) ( frac{4}{5}+frac{2}{3} )(g) ( frac{3}{4}-frac{1}{3} )(h) ( frac{5}{6}-frac{1}{3} )(i) ( frac{2}{3}+frac{3}{4}+frac{1}{2} )(j) ( frac{1}{2}+frac{1}{3}+frac{1}{6} )(k) ( 1 frac{1}{3}+3 frac{2}{3} )(l) ( 4 frac{2}{3}+3 frac{1}{4} )(m) ( frac{16}{5}-frac{7}{5} )(n) ( frac{4}{3}-frac{1}{2} )
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ... upto n terms in C++
- Following are the number of members in 25 families of a village:$6, 8, 7, 7, 6, 5, 3, 2, 5, 6, 8, 7, 7, 4, 3, 6, 6, 6, 7, 5, 4, 3, 3, 2, 5$Prepare a frequency distribution table for the data using class intervals $ 0-2, 2-4$, etc.
