C program to find the sum of ‘N’ natural numbers.

The natural numbers are the positive integers: 1, 2, 3, 4, 5, … Their sum from 1 to N can be computed two ways: a simple loop that adds each number, or the Gauss formula N×(N+1)/2 that gives the answer in O(1) time without any loop. This post shows both, explains why the formula works, and highlights an integer overflow trap that trips up beginners.

The original post used conio.h, void main(), and clrscr(). This rewrite compiles clean under gcc -ansi -Wall -Wextra.

C Program: Sum of N Natural Numbers — Loop + Gauss Formula

/* Sum of N natural numbers: loop method + Gauss formula
 * Natural numbers: 1, 2, 3, ..., N
 * Compile: gcc -ansi -Wall -Wextra sumN.c -o sumN */
#include <stdio.h>

int main(void)
{
    int i, n, loop_sum;
    long gauss_sum;

    printf("Enter N: ");
    if (scanf("%d", &n) != 1 || n < 1) {
        printf("Enter a positive integer.\n");
        return 1;
    }

    /* Method 1: loop */
    loop_sum = 0;
    for (i = 1; i <= n; i++)
        loop_sum += i;

    /* Method 2: Gauss formula — no loop needed */
    gauss_sum = (long)n * (n + 1) / 2;

    printf("\nSum of first %d natural numbers:\n", n);
    printf("  Loop method  : %d\n",  loop_sum);
    printf("  Gauss formula: %ld\n", gauss_sum);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra sumN.c -o sumN
./sumN

Sample Output

Enter N: 10

Sum of first 10 natural numbers:
  Loop method  : 55
  Gauss formula: 55

Enter N: 100

Sum of first 100 natural numbers:
  Loop method  : 5050
  Gauss formula: 5050

Why the Gauss Formula Works

Carl Friedrich Gauss (at age ~8, according to the legend) noticed that pairing numbers from both ends gives constant sums:

1 + 2 + 3 + ... + N
N + (N-1) + (N-2) + ... + 1

Each column sums to (N+1). There are N such pairs. But we have added the series twice, so divide by 2: sum = N × (N+1) / 2.

N Formula N(N+1)/2 Sum
5 5×6/2 15
10 10×11/2 55
100 100×101/2 5050
1000 1000×1001/2 500500

The Integer Overflow Trap

Why does the code use (long)n * (n + 1) / 2 instead of n * (n + 1) / 2?

On most 32-bit systems, int holds values up to 2,147,483,647 (INT_MAX). For N = 65,536 (which is within int range): n*(n+1) = 65,536 × 65,537 = 4,295,032,832 — this exceeds INT_MAX and overflows. The result would be garbage.

The cast (long)n promotes the multiplication to long before the overflow can happen. On most platforms long is 32 or 64 bits — check with sizeof(long) if you need guaranteed width; use long long for very large N.

Code Explanation

  • Loop method — straightforward accumulation: start sum=0, add each i from 1 to N. This takes O(n) time and is the most readable approach. For small N it’s negligibly slow; for N in the millions you’d prefer the formula.
  • Gauss formula — O(1): three operations regardless of N. For N=1,000,000, the loop takes a million additions; the formula computes the same result in three operations.
  • Validation: n >= 1 — natural numbers start at 1, not 0. The Gauss formula gives 0 for N=0 (mathematically valid as an empty sum), but the prompt says “positive integer”. If 0 should be allowed, remove the n < 1 guard.

What This Program Teaches

  • Closed-form formulas beat loops for mathematically regular sums — the loop approach is O(n), the Gauss formula is O(1). Knowing when a closed form exists is an important skill. Other examples: sum of first N odd numbers = N², sum of first N squares = N(N+1)(2N+1)/6.
  • Cast before multiplication to avoid overflow(long)n * (n+1) promotes before the multiply; (long)(n * (n+1)) casts the result after the overflow already happened. The position of the cast changes what gets computed.
  • %ld for long in printf — use %d for int, %ld for long, %lld for long long. Mismatching the format specifier and argument type is undefined behavior, even if it often “works” on little-endian 64-bit systems where long==int in practice.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>