Programming Examples

C Program to Calculate the Sum of Natural Numbers


Write a C Program to Calculate the Sum of Natural Numbers

Solution

#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;
}
Output

Enter a positive integer: 100

Sum = 5050

CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online Exam Quiz O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Best Website and Software Company in Allahabad Website development Company in Allahabad