Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n) - GeeksforGeeks

24 Mar, 2021
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++
C
Java
Python
C#
PHP
Javascript
// CPP program to calculate the following series#include<iostream>using namespace std; // Function to calculate the following seriesint Series(int n){    int i;    int sums = 0;    for (i = 1; i <= n; i++)        sums += (i * i);    return sums;} // Driver Codeint main(){    int n = 3;    int res = Series(n);    cout<<res<<endl;}
// C program to calculate the following series#include <stdio.h> // Function to calculate the following seriesint Series(int n){    int i;    int sums = 0;    for (i = 1; i <= n; i++)        sums += (i * i);    return sums;} // Driver Codeint main(){    int n = 3;    int res = Series(n);    printf("%d", res);}
// Java program to calculate the following seriesimport 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 program to calculate the following seriesdef Series(n):    sums = 0    for i in range(1, n + 1):        sums += (i * i);    return sums # Driver Coden = 3res = Series(n)print(res)
// C# program to calculate the following seriesusing 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 program to calculate// the following series // Function to calculate the// following seriesfunction 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.?>
<script> // Javascript program to calculate the following series     // Function to calculate the following series    function Series( n) {        let i;        let sums = 0;        for (i = 1; i <= n; i++)            sums += (i * i);        return sums;    }     // Driver Code              let n = 3;        let res = Series(n);        document.write(res); // This code contributed by Princi Singh </script>
Output : 
14
Time Complexity: O(n)Please refer below post for O(1) solution. Sum of squares of first n natural numbers 
jit_t
SURENDRA_GANGWAR
princi singh
number-theory
series
Mathematical
School Programming
number-theory
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Print all possible combinations of r elements in a given array of size n
Operators in C / C++
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects