C Program For Sum of Natural Numbers

Ram Pothuraju
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;   // sum = sum+i;
    }

    printf("Sum = %d",sum);

    return 0;
}



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

Enter a positive integer: 100
Sum = 5050

Post a Comment

0Comments

Post a Comment (0)