Sum of n natural numbers

 #include <stdio.h>


int main()
{
    int i = 1, sum = 0, n;
    printf("Enter the value of n\n");
    scanf("%d", &n);
    while (i <= n)
    {
        sum += i;
        i++;
    }
    printf("The sum of %d natural numbers is:%d", n, sum);
    return 0;
}

Comments

Popular posts from this blog

snake game Version-2 (using random number)

allocating dynamic memory using calloc

store a table in dynamic array and then change the size of array dynamically to store more numbers