Introduction
Summing squares of natural numbers is a classic example in mathematics and programming. This tutorial explains how to write a C program to evaluate the series 1² + 2² + 3² + … + n². This program uses loops and arithmetic operations to calculate the sum efficiently.
Understanding the Series
The formula for the sum of squares from 1 to n is:
Sum = 1² + 2² + 3² + … + n² = n(n + 1)(2n + 1) / 6
But in this blog, we’ll compute it using a loop to practice iteration and accumulation.
C Program to Evaluate the Series
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
sum += i * i;
}
printf("Sum of the series 1² + 2² + ... + %d² = %d\n", n, sum);
return 0;
}
Explanation of the Code
- Input: Accepts a positive integer
n
from the user. - Logic: A loop runs from 1 to
n
, squaring each number and adding it tosum
. - Output: Displays the result of the accumulated sum of squares.
Sample Output
Enter the value of n: 5
Sum of the series 1² + 2² + ... + 5² = 55
Real-World Applications
- Used in statistical calculations
- Physics-based simulations
- Performance analysis in algorithm loops
- Education and testing platforms
Conclusion
This C program to evaluate square series helps learners understand loop operations and summation logic. You can try both the manual loop and the formula approach to compare accuracy and efficiency.