Programming Code Center(PCC)
[C]

(PCC)::[how-to-write-C-Program-to-Calculate-the-Sum-of-Natural-Numbers]::[c]

File Name : Sum_Natural_Numbers.c

//Sum of Natural Numbers Using for Loop
#include <stdio.h>
int main() {
    int n, i, sum = 0;

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

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

    printf("Sum = %d", sum);
    return 0;
}

//Sum of Natural Numbers Using while Loop
#include <stdio.h>
int main() {
    int n, i, sum = 0;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    i = 1;

    while (i <= n) {
        sum += i;
        ++i;
    }

    printf("Sum = %d", sum);
    return 0;
}

Output :

Sum_Natural_Numbers.jpg