Skip to content
doubtly

1 answer

Best answer

Answer: Read N and add 1, 2, 3, ..., N in a loop (the result equals \(\frac{N(N+1)}{2}\)).

#include <stdio.h>

int main(void)
{
    int n, i;
    long sum = 0;

    printf("Enter N: ");
    scanf("%d", &n);

    for (i = 1; i <= n; i++)
        sum = sum + i;

    printf("Sum of first %d natural numbers = %ld\n", n, sum);
    return 0;
}

The loop runs i from 1 to N and adds each value to sum. For N = 10 the output is 55, which matches the formula \(\frac{10 \times 11}{2} = 55\). The user only enters N; the natural numbers themselves are generated by the loop.

Doubtly AI · 23 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.