You have been given a series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n), find out the sum of the series till nth term.
Examples :
Input : n = 3 Output : 14 Explanation : 1 + 1/2^2 + 1/3^3 Input : n = 5 Output : 55 Explanation : (1*1) + (2*2) + (3*3) + (4*4) + (5*5)
C++
// CPP program to calculate the following series #include<iostream> using namespace std; // Function to calculate the following series int Series(int n) { int i; int sums = 0; for (i = 1; i <= n; i++) sums += (i * i); return sums; } // Driver Code int main() { int n = 3; int res = Series(n); cout<<res<<endl; } |
C
// C program to calculate the following series #include <stdio.h> // Function to calculate the following series int Series(int n) { int i; int sums = 0; for (i = 1; i <= n; i++) sums += (i * i); return sums; } // Driver Code int main() { int n = 3; int res = Series(n); printf("%d", res); } |
Java
// Java program to calculate the following series import java.io.*; class GFG { // Function to calculate the following series static int Series(int n) { int i; int sums = 0; for (i = 1; i <= n; i++) sums += (i * i); return sums; } // Driver Code public static void main(String[] args) { int n = 3; int res = Series(n); System.out.println(res); } } |
Python
# Python program to calculate the following series def Series(n): sums = 0 for i in range(1, n + 1): sums += (i * i); return sums # Driver Code n = 3res = Series(n) print(res) |
C#
// C# program to calculate the following series using System; class GFG { // Function to calculate the following series static int Series(int n) { int i; int sums = 0; for (i = 1; i <= n; i++) sums += (i * i); return sums; } // Driver Code public static void Main() { int n = 3; int res = Series(n); Console.Write(res); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to calculate // the following series // Function to calculate the // following series function Series($n) { $i; $sums = 0; for ($i = 1; $i <= $n; $i++) $sums += ($i * $i); return $sums; } // Driver Code $n = 3; $res = Series($n); echo($res); // This code is contributed by Ajit. ?> |
Output :
14
Time Complexity: O(n)
Please refer below post for O(1) solution.
Sum of squares of first n natural numbers
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
- Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n
- Program to find Sum of the series 1*3 + 3*5 + ....
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Program to find sum of harmonic series
- Program to find the sum of the series 1 + x + x^2+ x^3+ .. + x^n
- Program for sum of geometric series
- Program for sum of arithmetic series
- Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
- Program for sum of cos(x) series
- Program to print the Sum of series -1 + 2 + 11 + 26 + 47 +.....
- Program to find Nth term in the given Series
- Program to find N-th term of the series a, b, b, c, c, c,.......
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find Nth term of series 1, 3, 12, 60, 360…
- Program to find the Nth term of series -1, 2, 11, 26, 47......
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.

