Powered By Blogger

Friday, December 24, 2010

C Program to find the sum of first n natural numbers

/* C Program to Print the sum of the first N natural numbers. The value of N is accepted from
the user */

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int N;
    int ctr;
    int sum=0;
    printf("Enter the value of N\n");
    scanf("%d",&N);

    for(ctr=1;ctr<=N;ctr++)
    {
        sum=sum+ctr;
    }
    printf("The sum of the first %d natural numbers is %d",N,sum);
    return 0;
}


No comments:

Post a Comment