Sum of Series 1^2+2^2+…+n^2 in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int main() {
    int n, sum = 0;
    printf("Enter n: ");
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        sum += i * i;
    printf("Sum of series: %d\n", sum);
    return 0;
}

C Output

Input:
Enter n: 5

Output:
Sum of series: 55


C++ Program

#include <iostream>
using namespace std;
int main() {
    int n, sum = 0;
    cout << "Enter n: ";
    cin >> n;
    for(int i = 1; i <= n; i++)
        sum += i * i;
    cout << "Sum of series: " << sum << endl;
    return 0;
}

C++ Output

Input:
Enter n: 7

Output:
Sum of series: 140


JAVA Program

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n, sum = 0;
        System.out.print("Enter n: ");
        n = sc.nextInt();
        for (int i = 1; i <= n; i++)
            sum += i * i;
        System.out.println("Sum of series: " + sum);
    }
}

JAVA Output

Input:
Enter n: 4

Output:
Sum of series: 30


Python Program

n = int(input("Enter n: "))
sum_sq = sum(i * i for i in range(1, n + 1))
print("Sum of series:", sum_sq)

Python Output

Input:
Enter n: 6

Output:
Sum of series: 91


In-Depth Learning – Entire Concept in Paragraphs
Example
If n = 5, the sum will be:
1² + 2² + 3² + 4² + 5²
= 1 + 4 + 9 + 16 + 25
= 55

The algorithm operates by beginning at 1, squaring each number through to n, and adding it to the total. The for loop guarantees we cover each term once.

Real-Life Analogy
Suppose you have a staircase whose first step contains 1 tile, the second 4 tiles, the third 9 tiles, and so on. The rule is that each step contains tiles equal to the square of the step number. If you need to find how many tiles all steps contain altogether, you simply add up these squares — which is precisely what this program does.

Why It Matters
Summing squares isn't purely an academic exercise — it shows up in statistics (sum of squares in variance calculation), physics (energy equations, distances), and computer graphics (distance computation in 3D space). It's also applied to algorithm analysis, where the sum of squares might be used to describe the complexity of some operations.

Mathematical Insight

There’s actually a formula for the sum of squares:

S=n(n+1)(2n+1)6S = \frac{n(n+1)(2n+1)}{6}

For n = 5:

S=5(6)(11)6=55S = \frac{5(6)(11)}{6} = 55

Using this formula makes the calculation instant, without loops — which is faster for very large n.


Learning Insights
You learn summation methods using loops.

You observe pattern recognition (seeing square numbers).

You learn the mathematical formula to achieve better performance.

You know how an actual-world repetitive pattern can be calculated efficiently.

SEO-Optimized Closing
Learning how to calculate the sum of the series 1² + 2² + … + n² is a fundamental programming and mathematical skill. Whether you’re preparing for coding interviews, competitive exams, or practical applications in data analysis and physics, mastering this problem builds strong logic and optimization skills. With both the loop-based method and the mathematical formula, you’re equipped to solve it in any situation quickly and efficiently.