Q:

C program to calculate sum of first N natural numbers.

0

C program to calculate sum of first N natural numbers.

This program will read the value of N and calculate sum from 1 to N, first N natural numbers means numbers from 1 to N and N will be read through user.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Sum of first N natural numbers using C program

/*C program to calculate sum of first N natural numbers.*/

#include <stdio.h>

int main()
{
    int n, i;
    int sum;

    printf("Enter the value of N: ");
    scanf("%d", &n);

    sum = 0;

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

    printf("Sum is: %d\n", sum);

    return 0;
}

Output

RUN 1:
Enter the value of N: 100
Sum is: 5050

RUN 2:
Enter the value of N: 10
Sum is: 55

RUn 3:
Enter the value of N: 3
Sum is: 6

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to print all prime numbers from 1 to N.... >>
<< C program to find factorial of a number...