Cal11 calculator

C Program to Calculate Summation N 2

Reviewed by Calculator Editorial Team

Introduction

Calculating the summation of n² (n squared) is a common mathematical operation in programming. This guide will show you how to write a C program to perform this calculation, including the formula, code implementation, and practical examples.

The summation of n² is calculated by adding together the squares of all integers from 1 to n. This operation is useful in various mathematical and programming contexts, including algorithm analysis and mathematical proofs.

Formula

The formula for the summation of n² is:

Σ(n²) = 1² + 2² + 3² + ... + n²

There's also a mathematical formula that can calculate this sum directly without iteration:

Σ(n²) = n(n + 1)(2n + 1)/6

We'll implement both approaches in our C program to demonstrate different calculation methods.

C Program

Here's a complete C program that calculates the summation of n² using both iterative and formula-based approaches:

#include <stdio.h>

// Function to calculate summation using iteration
long long sumSquaresIterative(int n) {
    long long sum = 0;
    for (int i = 1; i <= n; i++) {
        sum += i * i;
    }
    return sum;
}

// Function to calculate summation using formula
long long sumSquaresFormula(int n) {
    return (long long)n * (n + 1) * (2 * n + 1) / 6;
}

int main() {
    int n;

    printf("Enter a positive integer n: ");
    scanf("%d", &n);

    if (n <= 0) {
        printf("Please enter a positive integer.\n");
        return 1;
    }

    long long sumIterative = sumSquaresIterative(n);
    long long sumFormula = sumSquaresFormula(n);

    printf("\nResults for n = %d:\n", n);
    printf("Iterative method: %lld\n", sumIterative);
    printf("Formula method: %lld\n", sumFormula);

    return 0;
}

The program includes two functions:

  • sumSquaresIterative - Calculates the sum by iterating through each number from 1 to n and adding its square
  • sumSquaresFormula - Uses the mathematical formula to calculate the sum directly

Both methods should produce the same result, but the formula method is generally more efficient for large values of n.

Example

Let's calculate the summation of n² for n = 5:

1² + 2² + 3² + 4² + 5² = 1 + 4 + 9 + 16 + 25 = 55

Using the formula:

Σ(5²) = 5 × 6 × 11 / 6 = 55

Both methods correctly calculate the sum as 55 for n = 5.

FAQ

What is the difference between the iterative and formula methods?

The iterative method calculates the sum by adding each square individually, while the formula method uses a direct mathematical calculation. The formula method is generally more efficient for large values of n.

Can I use this program for very large values of n?

Yes, but be aware that very large values of n might cause integer overflow. The program uses long long to handle larger numbers, but extremely large values could still cause issues.

Is there a mathematical proof for the formula?

Yes, the formula can be derived using mathematical induction or by recognizing the pattern in the sums of squares. This formula is well-established in mathematical literature.