- 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

# C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2

There are many types of series in mathematics which can be solved easily in C programming. This program is to find the sum of following of series in C program.

T_{n}= n^{2}- (n-1)^{2}

Find the sum of all of the terms of series as Sn mod (10^{9} + 7) and,

S_{n} = T_{1} + T_{2} + T_{3} + T_{4} + ...... + T_{n}

Input: 229137999 Output: 218194447

### Explanation

Tn can be expressed as 2n-1 to get it

As we know ,

=> Tn = n2 - (n-1)2 =>Tn = n2 - (1 + n2 - 2n) =>Tn = n2 - 1 - n2 + 2n =>Tn = 2n - 1. find ∑Tn. ∑Tn = ∑(2n – 1) Reduce the above equation to, =>∑(2n – 1) = 2*∑n – ∑1 =>∑(2n – 1) = 2*∑n – n. here, ∑n is the sum of first n natural numbers. As known the sum of n natural number ∑n = n(n+1)/2. Now the equation is, ∑Tn = (2*(n)*(n+1)/2)-n = n2 The value of n2 can be large. Instead of using n2 and take the mod of the result. So, using the property of modular multiplication for calculating n2: (a*b)%k = ((a%k)*(b%k))%k

## Example

#include <iostream> using namespace std; #define mod 1000000007 int main() { long long n = 229137999; cout << ((n%mod)*(n%mod))%mod; return 0; }

- Related Articles
- Find sum of Series with n-th term as n^2 - (n-1)^2 in C++
- Java Program to Find sum of Series with n-th term as n^2 – (n-1)^2
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find N-th term of series 1, 2, 11, 12, 21… 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!
- Sum of the series 1.2.3 + 2.3.+ … + n(n+1)(n+2) in C
- C++ program to find n-th term of series 2, 10, 30, 68, 130 …
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- C++ Programe to find n-th term in series 1 2 2 3 3 3 4
- Program to find N-th term of series 0, 0, 2, 1, 4, 2, 6, 3, 8…in C++
- C++ program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + … + n/a^n)
- Program to find N-th term of series 2, 4, 3, 4, 15… in C++

Advertisements