C program to calculate sum of first N natural numbers.

Problem statement

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.

C program to calculate sum of first N natural numbers

/*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

C Looping Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.