Find Sum of Series 1^2 – 2^2 + 3^2 – 4^2 ….. upto n terms
Given a number n, the task is to find the sum of the below series upto n terms:
12 – 22 + 32 – 42 + …..
Examples:
Input: n = 2
Output: -3
Explanation:
sum = 12 - 22
= 1 - 4
= -3
Input: n = 3
Output: 6
Explanation:
sum = 12 - 22 + 32
= 1 - 4 + 9
= 6
Naive Approach:
This method involves simply running a loop of i from 1 to n and if i is odd then simply add its square to the result it i is even then simply subtract square of it to the result.
Below is the implementation of the above approach:
C++
// C++ program to find sum of series // 1^2 - 2^2 + 3^3 - 4^4 + ... #include <bits/stdc++.h> using namespace std; // Function to find sum of series int sum_of_series(int n) { int result = 0; for (int i = 1; i <= n; i++) { // If i is even if (i % 2 == 0) result = result - pow(i, 2); // If i is odd else result = result + pow(i, 2); } // return the result return result; } // Driver Code int main(void) { // Get n int n = 3; // Find the sum cout << sum_of_series(n) << endl; // Get n n = 10; // Find the sum cout << sum_of_series(n) << endl; } |
Java
// Java Program to find sum of series // 1^2 - 2^2 + 3^3 - 4^4 + ... import java.util.*; import java.lang.*; class GFG { // Function to find sum of series static int sum_of_series(int n) { int result = 0; for (int i = 1; i <= n; i++) { // If i is even if (i % 2 == 0) result = result - (int)Math.pow(i, 2); // If i is odd else result = result + (int)Math.pow(i, 2); } // return the result return result; } // Driver Code public static void main(String args[]) { // Get n int n = 3; // Find the sum System.out.println(sum_of_series(n)); // Get n n = 10; // Find the sum System.out.println(sum_of_series(n)); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
Python3
# Python3 program to find sum of series # 1^2 - 2^2 + 3^3 - 4^4 + ... # Function to find sum of series def sum_of_series(n): result = 0 for i in range(1, n + 1) : # If i is even if (i % 2 == 0): result = result - pow(i, 2) # If i is odd else: result = result + pow(i, 2) # return the result return result # Driver Code if __name__ == "__main__": # Get n n = 3 # Find the sum print(sum_of_series(n)) # Get n n = 10 # Find the sum print(sum_of_series(n)) # This code is contributed # by ChitraNayal |
C#
// C# Program to find sum of series // 1^2 - 2^2 + 3^3 - 4^4 + ... using System; class GFG { // Function to find sum of series static int sum_of_series(int n) { int result = 0; for (int i = 1; i <= n; i++) { // If i is even if (i % 2 == 0) result = result - (int)Math.Pow(i, 2); // If i is odd else result = result + (int)Math.Pow(i, 2); } // return the result return result; } // Driver Code public static void Main() { // Get n int n = 3; // Find the sum Console.WriteLine(sum_of_series(n)); // Get n n = 10; // Find the sum Console.WriteLine(sum_of_series(n)); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
PHP
<?php // PHP program to find sum of series // 1^2 - 2^2 + 3^3 - 4^4 + ... // Function to find sum of series function sum_of_series($n) { $result = 0; for ($i = 1; $i <= $n; $i++) { // If i is even if ($i % 2 == 0) $result = $result - pow($i, 2); // If i is odd else $result = $result + pow($i, 2); } // return the result return $result; } // Driver Code // Get n $n = 3; // Find the sum echo sum_of_series($n),"\n"; // Get n $n = 10; // Find the sum echo sum_of_series($n),"\n"; // This Code is Contributed by anuj_67 ?> |
6 -55
Time Complexity: Complexity of above stated code is O(n).
Efficient Approach
It is based on condition of n
If n is even:
If n is odd:
Below is the implementation of the above approach:
C++
// C++ Program to find sum of series // 1^2 - 2^2 +3^3 -4^4 + ... #include <bits/stdc++.h> using namespace std; // Function to find sum of series int sum_of_series(int n) { int result = 0; // If n is even if (n % 2 == 0) { result = -(n * (n + 1)) / 2; } // If n is odd else { result = (n * (n + 1)) / 2; } // return the result return result; } // Driver Code int main(void) { // Get n int n = 3; // Find the sum cout << sum_of_series(n) << endl; // Get n n = 10; // Find the sum cout << sum_of_series(n) << endl; } |
Java
// Java Program to find sum of series // 1^2 - 2^2 +3^3 -4^4 + ... import java.util.*; import java.lang.*; class GFG { // Function to find sum of series static int sum_of_series(int n) { int result = 0; // If n is even if (n % 2 == 0) { result = -(n * (n + 1)) / 2; } // If n is odd else { result = (n * (n + 1)) / 2; } // return the result return result; } // Driver Code public static void main(String args[]) { // Get n int n = 3; // Find the sum System.out.println(sum_of_series(n)); // Get n n = 10; // Find the sum System.out.println(sum_of_series(n)); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
Python3
# Python3 Program to find sum of series # 1^2 - 2^2 +3^3 -4^4 + ... # Function to find sum of series def sum_of_series(n) : result = 0 # If n is even if (n % 2 == 0) : result = -(n * (n + 1)) // 2 # If n is odd else : result = (n * (n + 1)) // 2 # return the result return result # Driver Code if __name__ == "__main__" : # Get n n = 3 # Find the sum print(sum_of_series(n)) # Get n n = 10 # Find the sum print(sum_of_series(n)) # This code is contributed by Ryuga |
C#
// C# Program to find sum of series // 1^2 - 2^2 +3^3 -4^4 + ... using System; class GFG { // Function to find sum of series static int sum_of_series(int n) { int result = 0; // If n is even if (n % 2 == 0) { result = -(n * (n + 1)) / 2; } // If n is odd else { result = (n * (n + 1)) / 2; } // return the result return result; } // Driver Code public static void Main() { // Get n int n = 3; // Find the sum Console.WriteLine(sum_of_series(n)); // Get n n = 10; // Find the sum Console.WriteLine(sum_of_series(n)); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
PHP
<?php // PHP program to find sum of series // 1^2 - 2^2 +3^3 -4^4 + ... // Function to find sum of series function sum_of_series($n) { $result = 0; // If n is even if ($n % 2 == 0) { $result = -($n * ($n + 1)) / 2; } // If n is odd else { $result = ($n * ($n + 1)) / 2; } // return the result return $result; } // Driver Code // Get n $n = 3; // Find the sum echo sum_of_series($n); echo ("\n"); // Get n $n = 10; // Find the sum echo sum_of_series($n); echo ("\n"); // Get n $n = 10; // This code is contributed // by Shivi_Aggarwal ?> |
6 -55
Recommended Posts:
- Find sum of the series ?3 + ?12 +......... upto N terms
- Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms
- Find the sum of series 3, -6, 12, -24 . . . upto N terms
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Find the sum of the series 1+11+111+1111+..... upto n terms
- Find sum of the series 1+22+333+4444+...... upto n terms
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.
- Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
- Find n terms of Fibonacci type series with given first two terms
- Find the sum of all the terms in the n-th row of the given series
- Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
- Find the sum of n terms of the series 1,8,27,64 ....
- Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, ...
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





